From 5b8f54b0cb64c2323350c26b79c4890433fbfe8d Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Wed, 17 Sep 2025 23:01:11 +0200 Subject: [PATCH] vim:keys: Fix newline and tab search for v_* Multiline search didn't work previously when searching with v_* and tabs were ignored when searching with v_*. --- .config/vim/vimrc.d/40-keys.vim | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/.config/vim/vimrc.d/40-keys.vim b/.config/vim/vimrc.d/40-keys.vim index a343778..68026cf 100644 --- a/.config/vim/vimrc.d/40-keys.vim +++ b/.config/vim/vimrc.d/40-keys.vim @@ -109,25 +109,33 @@ endif " Search for selected text. " Modified from https://vim.fandom.com/wiki/Search_for_visually_selected_text -function! GetVisualSelection() +function! GetVisualSelection(escape = "", byteescape = 'n') let l:old_reg = getreg('"') let l:old_regtype = getregtype('"') norm gvy let l:sel = getreg('"') call setreg('"', l:old_reg, l:old_regtype) + + let l:sel = l:sel->escape(a:escape) + for l:char in a:byteescape + let l:sel = l:sel->substitute('\'..l:char, '\\'..l:char, 'g') + endfor + return l:sel endfunction -vmap * /\V=escape(GetVisualSelection(),'/\') -vmap # ?\V=escape(GetVisualSelection(),'?\') +vmap * /\V=GetVisualSelection('/\') +vmap # ?\V=GetVisualSelection('?\') " Extended `*`. Starts vim search (without jump) and ripgrep nmap * :let @/ = '\<' . expand('') . '\>' \ set hlsearch \ Rg \b=expand('')\b -vmap * :let @/ = "\\V=escape(escape(GetVisualSelection(), '\'), '"\')" +" TODO: pass --multiline to rg when multiple lines selected +" TODO: Use ^ and $ anchors in visual-line mode +vmap * :let @/ = "\\V=escape(GetVisualSelection('\'), '"\')" \ set hlsearch - \ Rg =escape(GetVisualSelection(), '.\[]*+?{}^$()') + \ Rg =GetVisualSelection('.\[]*+?{}^$()', 'nt') nmap g* :let @/ = expand('') \ set hlsearch \ Rg =expand('') @@ -439,7 +447,7 @@ vnoremap gu " `ExpandVisualSelection(1)` which results in a block selection that spans over " all other TODOs as well. function! ExpandVisualSelection(direction) - let l:sel = escape(GetVisualSelection(), '\') + let l:sel = GetVisualSelection('\') normal gv " Move the cursor onto the side of the selection that points in the