vim:keys: Add <leader>* vmap to ripgrep selection

Similar to <leader>* in normal mode, set the search pattern to the
visual selection and start a ripgrep search.

To not mess up the commandline, potential quotes that are the same as
the ones used around the string need to be escaped. As this is only
possible with double quotes, a second layer of backslash escaping is
needed as well.

Because of that, **before** escaping backslashes and double quotes, all
backslashes need to be escaped a second time. The other way around would
result in two backslashes in front of each double quote, which would
miss the whole point entirely.

In addition GetVisualSelection needed to be expanded to first reselect
the visual selection for it to work in the new mapping.
This commit is contained in:
2023-02-15 14:25:17 +01:00
parent 1933218756
commit 1d58eb729d

View File

@@ -91,7 +91,7 @@ endif
function! GetVisualSelection()
let l:old_reg = getreg('"')
let l:old_regtype = getregtype('"')
norm y
norm gvy
let l:sel = getreg('"')
call setreg('"', l:old_reg, l:old_regtype)
return l:sel
@@ -100,10 +100,13 @@ endfunction
vmap * /\V<C-R>=escape(GetVisualSelection(),'/\')<CR><CR>
vmap # ?\V<C-R>=escape(GetVisualSelection(),'?\')<CR><CR>
" Extended `*`. Starts vim search (without jump) and ripgrep search for cword
" Extended `*`. Starts vim search (without jump) and ripgrep
nmap <leader>* :let @/ = '\<' . expand('<cword>') . '\>' <bar>
\ set hlsearch <bar>
\ Rg \b<C-R>=expand('<cword>')<CR>\b<CR>
vmap <leader>* :<C-U>let @/ = "\\V<C-R>=escape(escape(GetVisualSelection(), '\'), '"\')<CR>" <bar>
\ set hlsearch <bar>
\ Rg <C-R>=escape(GetVisualSelection(), '.\[]<bar>*+?{}^$()')<CR><CR>
" Search inside visual selection
noremap <leader>v/ /\%V