From edb11e5e1dc73e3f239f0c8b20c3f78352ef8ed9 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sat, 14 May 2022 09:10:13 +0530 Subject: [PATCH] nvim: plugin/visual-star-search: Use custom visual star search neovim enabled search selection by * and # in visual mode but seems that is not good enough. https://github.com/neovim/neovim/pull/18538#pullrequestreview-971039192 Add and use Justin Keyes version referenced above. --- .../nvim/plugin/visual-star-search.vim | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 nvim/.config/nvim/plugin/visual-star-search.vim diff --git a/nvim/.config/nvim/plugin/visual-star-search.vim b/nvim/.config/nvim/plugin/visual-star-search.vim new file mode 100644 index 0000000..198fa55 --- /dev/null +++ b/nvim/.config/nvim/plugin/visual-star-search.vim @@ -0,0 +1,23 @@ +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 =join(get_visual_selection_list(), " ") +inoremap =join(get_visual_selection_list(), " ") + +xnoremap * ms/\V=get_visual_selection_searchpattern() +nnoremap * ms:let @/='\V\<'.escape(expand(''), '/\').'\>'call histadd('/',@/)set hlsearch +nnoremap g* ms:let @/='\V' . escape(expand(''), '/\') call histadd('/',@/)set hlsearch