Skip to content

Instantly share code, notes, and snippets.

@dr0bz
Last active June 2, 2023 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dr0bz/2932b41ee57b8dfeca161e77b2572354 to your computer and use it in GitHub Desktop.
Save dr0bz/2932b41ee57b8dfeca161e77b2572354 to your computer and use it in GitHub Desktop.
Vim grep wrapper for ripgrep (rg). Features: quickfix window match highlighting, regex, path and rg arguments.
" :Grep "Peding Transaction" " with fixed strings
" :Grep! "\sTransaction" " regex
" :Grep \$stateParam
" :Grep "Pending Transaction" -g "*.php" " with rg parameters
" :Grep "Pending Transaction" vendor/ " with path
" :Grep "Pending Transaction" -g "*.php" vendor " with rg parameters and path
" rg as :grep
set grepprg=rg\ --vimgrep\ --follow\ --hidden
set grepformat^=%f:%l:%c:%m
hi QuickFixMatch guifg=#839e00
function! s:Grep(isFixed, args) abort
let fixedArg = a:isFixed ? '' : '-F'
let argArray = split(a:args)
let restArgs = ''
let quotedStringPattern = '^\".\{-}\"'
let pattern = matchstr(a:args, quotedStringPattern) " check for first pattern with space in quotes
if !empty(pattern)
let matchPattern = substitute(pattern, '"', '', 'g')
let restArgs = substitute(a:args, quotedStringPattern, '', '')
else
let pattern = argArray[0]
let matchPattern = pattern
let restArgs = join(argArray[1:], ' ')
endif
execute 'silent! grep! '. fixedArg . ' ' . pattern . ' ' . restArgs . ' | cwindow 20 | redraw! | match QuickFixMatch /' . matchPattern . '/'
endfunction
command! -nargs=+ -complete=file -bang Grep silent! call s:Grep(<bang>0, <q-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment