From 9e07b24cf70b4522940888cff4a87e9f068d413a Mon Sep 17 00:00:00 2001 From: Michael Campagnaro Date: Sun, 22 Jan 2023 08:34:29 -0500 Subject: [PATCH] Escape some characters in vim rip-grep search --- vimrc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/vimrc b/vimrc index 8df4979..5fe53f5 100644 --- a/vimrc +++ b/vimrc @@ -197,7 +197,7 @@ call plug#begin('~/.vim/plugged') " MISC "//////////////////////////////////////////////////////////////// -Plug 'bling/vim-airline' " Enhanced status/tabline. +Plug 'vim-airline/vim-airline' " Enhanced status/tabline. Plug 'embear/vim-localvimrc' " Add a .lvimrc to a folder to override .vimrc config. Plug 'tpope/vim-obsession' " Continuously updated session files (tracks window positions, open folds, etc). Plug 'tpope/vim-fugitive' " Git wrapper (I particularly like :Gblame, which I've wrapped as :Blame) @@ -352,7 +352,9 @@ set wildmenu set wildmode=longest,list,full set wildignore+=*/log/*,*.so,*.swp,*.zip,*/rdoc/* -set grepprg=rg\ --vimgrep " Requires ripgrep to be installed. +if executable('rg') + set grepprg=rg\ --vimgrep\ --hidden " Requires ripgrep to be installed. +endif set list listchars=tab:»·,trail:· " Show trailing whitespace. @@ -1419,15 +1421,20 @@ function! Search(path, search_args, case_insensitive=0) return endif - "@note --pretty (i.e. colors) is not enabled in vim-ripgrep because the - "quickfix window doesn't seem to parse the ansi color codes. + " @note --pretty (i.e. colors) is not enabled in vim-ripgrep because the + " quickfix window doesn't seem to parse the ansi color codes. let l:rg_args = "--column --line-number --no-heading --fixed-strings --no-ignore --hidden --follow --trim -g \"!tags\" -g \"!.git/\" -g \"!AppData/\" " . a:search_args if a:case_insensitive let l:rg_args .= " --ignore-case" endif - exec 'Rg ' . l:rg_args . ' -e "' . l:term . '"' . ' ' . a:path + " Some characters need to be escaped. + let l:escaped_term = substitute(l:term, '[#%]', '\\\\\\&', 'g') + + let l:format = 'Rg ' . l:rg_args . ' ' . a:path . ' -e %s' + let l:cmd = printf(l:format, shellescape(l:escaped_term)) + exec l:cmd endfunction """""""""""""""""""""""""""""""""""""""""""""""""""""""