I want to redefine * such that if next match is not found, it
does # instead (goes to previous match).
Also, I want to redefine # so that is previoue match is not found,
it does * instead.
How to do do it ? I want it to be independent of 'wrapscan'
option (I have 'nowrapscan' always, anyway).
I think this only makes sense in the case where 'nowrapscan' is
set. Otherwise, there will always be a next/previous item to
find by just wrapping around again.
A first-pass try at such an effort:
nnoremap <silent> * :if !search('\<'.escape(expand('<cword>'),
'~^$\\.*[').'\>', 'Ws') <bar>call
search('\<'.escape(expand('<cword>'), '~^$\\.*[').'\>',
'Wsb')<bar>endif<cr>
nnoremap <silent> # :if !search('\<'.escape(expand('<cword>'),
'~^$\\.*[').'\>', 'Wsb') <bar>call
search('\<'.escape(expand('<cword>'), '~^$\\.*[').'\>',
'Ws')<bar>endif<cr>
Seemed to do something fairly close to what you describe. I
don't know if I got the whole suite of characters to escape. One
really only needs to escape characters that would appear in
'iskeyword', but as that set is user-modifiable, it would be
better to escape every possible candidate. If you have funky
characters in your iskeyword that trigger hiccups in the search()
function, just add them to the sets of characters that need to be
escaped:
~^$\\.*[
-tkc