dotfiles/nvim/.config/nvim/plugin/visual-star-search.vim
Sanchayan Maity d4f81fe865 nvim: plugin/visual-star-search: Set mark when doing backward search
While at it, drop calling set hlsearch for * and g* as hlsearch is
enabled by default and does not seem to be required.
2022-05-18 09:09:23 +05:30

28 lines
1.1 KiB
VimL

func! s:get_visual_selection_list() abort
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - (&selection ==? 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
return lines
endf
func! s:get_visual_selection_searchpattern() abort
let lines = s:get_visual_selection_list()
let lines = map(lines, 'escape(v:val, ''/\'')')
" Join with a _literal_ \n to make a valid search pattern.
return join(lines, '\n')
endf
" Read last visual-selection into command line
cnoremap <c-r><c-v> <c-r>=join(<sid>get_visual_selection_list(), " ")<CR>
inoremap <c-r><c-v> <c-r>=join(<sid>get_visual_selection_list(), " ")<CR>
xnoremap * <esc>ms/\V<c-r>=<sid>get_visual_selection_searchpattern()<CR><CR>
nnoremap <silent> * ms:<c-u>let @/='\V\<'.escape(expand('<cword>'), '/\').'\>'<Bar>call histadd('/',@/)<CR>
nnoremap <silent> g* ms:<c-u>let @/='\V' . escape(expand('<cword>'), '/\') <Bar>call histadd('/',@/)<CR>
" Use the setting of mark trick for backward search as well.
nnoremap <silent> # ms#<CR>
nnoremap <silent> g# msg#<CR>