Do "n" always look forward, regardless of whether / or? was used to search

I almost always search in Vim with / , and then continue looking forward with n and back with n . Sometimes, however, do I use ? to go to an element there are only a few lines above the line that I am currently in, and in this case, if I want to look for the same element forward, I must use n instead of of n , an annoying blow to mental speed.

So my question is: is it possible that n always goes forward and n always goes back?

PS The documentation seems to hint that this is not possible, since n just "Repeating the last" / "or"? "[Count] times," but who knows.

+7
vim editor shortcut
source share
2 answers

This was taken from the ZyX post on the vim mailing list.

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

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

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

 noremap <expr> nn noremap <expr> NN 

and when v:searchfoward == 0 (backward search), the mappings are equivalent

 noremap <expr> nn noremap <expr> NN 

This display works in standby, visibility and operator modes.


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

 noremap <expr> n (v:searchforward ? 'n' : 'N') noremap <expr> N (v:searchforward ? 'N' : 'n') 
+9
source share

This is as it is, despite some debate on the developer mailing list ...

Here is what you can do about it:

 :noremap n /<CR> :noremap N ?<CR> 

Relevant mailing list with much more interesting information: https://groups.google.com/d/msg/vim_dev/8jSoA931P2Y/JklYtxFOiAYJ

+6
source share

All Articles