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')
Fdinoff
source share