Wiki

by yszheda

View project onGitHub

Tip 86: Search for the Current Visual Selection

Return to the top: <<Practical Vim>>

xnoremap * :<C-u>call <SID>VSetSearch()<CR>/<C-R>=@/<CR><CR>
xnoremap # :<C-u>call <SID>VSetSearch()<CR>?<C-R>=@/<CR><CR>
function! s:VSetSearch()
let temp = @s
norm! gv"sy
let @/ = '\V' . substitute(escape(@s, '/\'), '\n', '\\n', 'g')
let @s = temp
endfunction

xnoremap keyword specifies that the mappings should apply to Visual mode but not to Select mode.

References:

# If you open the file in vim,
# then you can \"ayy@a the next line to execute the vim command: 
:h mapmode-x