From 1d58eb729d32161941f9a75078d4e50f9d9dc82c Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Wed, 15 Feb 2023 14:25:17 +0100 Subject: [PATCH] vim:keys: Add * vmap to ripgrep selection Similar to * 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. --- .config/vim/vimrc.d/40-keys.vim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.config/vim/vimrc.d/40-keys.vim b/.config/vim/vimrc.d/40-keys.vim index 09b5adf..0049703 100644 --- a/.config/vim/vimrc.d/40-keys.vim +++ b/.config/vim/vimrc.d/40-keys.vim @@ -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=escape(GetVisualSelection(),'/\') vmap # ?\V=escape(GetVisualSelection(),'?\') -" Extended `*`. Starts vim search (without jump) and ripgrep search for cword +" Extended `*`. Starts vim search (without jump) and ripgrep nmap * :let @/ = '\<' . expand('') . '\>' \ set hlsearch \ Rg \b=expand('')\b +vmap * :let @/ = "\\V=escape(escape(GetVisualSelection(), '\'), '"\')" + \ set hlsearch + \ Rg =escape(GetVisualSelection(), '.\[]*+?{}^$()') " Search inside visual selection noremap v/ /\%V