无论是否是/,都使'n'总是向前搜索。用于搜索 [英] Make 'n' always search forwards, regardless of whether / or ? was used for searching

查看:79
本文介绍了无论是否是/,都使'n'总是向前搜索。用于搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎总是在Vim中使用 / 进行搜索,然后继续使用 n 进行向前搜索,并使用<$进行向后搜索c $ c> N 。但是,有时候我会使用跳转到我当前所在行上方几行的项目,在这种情况下,如果我想搜索相同的项目项目转发,我必须使用 N 而不是 n ,这是一个烦人的心理减速。

I almost always search in Vim with /, and then continue searching forwards with n and backwards with N. Sometimes, however, I use ? to jump to an item just a few lines above the line I'm currently at, and in that case, if I want to search the same item forwards, I have to use N instead of n, an annoying mental speed bump.

所以我的问题是:是否可以使 n 总是前进,而 N 向后吗?

So my question is: is it possible to make n always go forwards, and N backwards?

PS文档似乎暗示这是不可能的,因为 n 只是重复最近的 /或? [count]次,但谁知道呢。

P.S. Documentation seems to hint that it's not possible, since n simply "Repeats the latest "/" or "?" [count] times", but who knows.

推荐答案

这取自 ZyX在vim邮件列表上的帖子

noremap <expr> n 'Nn'[v:searchforward]
noremap <expr> N 'nN'[v:searchforward]

它映射 n 通过将'Nn'索引为变量,从而基于变量 v:searchforward 到原始N或n两个元素的列表。这仅适用,因为映射是非递归的。如果它是递归的,则将其称为自身,您将陷入无限循环。

It maps n to the original N or n based off of the variable v:searchforward by indexing 'Nn' as a list of two elements. This only works because the mapping is non-recursive. If it was recursive this would call it self and you would be in an infinite loop.

v:searchforward == 1 (向前搜索)映射等效于

When v:searchforward == 1 (search forward) the mappings are equivalent to

noremap <expr> n n
noremap <expr> N N

,当 v:searchfoward == 0 (向后搜索)映射等效于

and when v:searchfoward == 0 (search backwards) the mappings are equivalent to

noremap <expr> n N
noremap <expr> N n

此映射在正常,可视和操作员暂挂模式下有效。

This mapping works in Normal, Visual, and Operator pending modes.

我可能会这样写。 (尽管我不确定这是否更清晰)

I probably would have written it like this. (Although I'm not sure this is any clearer)

noremap <expr> n (v:searchforward ? 'n' : 'N')
noremap <expr> N (v:searchforward ? 'N' : 'n')

这篇关于无论是否是/,都使'n'总是向前搜索。用于搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆