You have to forgive me, I canโt remember where I found this, but it works wonders. Just add to .vimrc . Use typical * and # after visual selection:
" Search for selected text, forwards or backwards. vnoremap <silent> * :<CU> \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR> \gvy/<CR><CR>=substitute( \escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR> \gV:call setreg('"', old_reg, old_regtype)<CR> vnoremap <silent> # :<CU> \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR> \gvy?<CR><CR>=substitute( \escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR> \gV:call setreg('"', old_reg, old_regtype)<CR>
The real advantage here is that it eludes any special characters, rather than just inserting it into the search command. It is also less than keystrokes, which maximizes the vimness of your task.
kenny
source share