Skip to content

Instantly share code, notes, and snippets.

@andlrc
Created May 2, 2023 17:34
Show Gist options
  • Select an option

  • Save andlrc/c8e1a3b9c1ec5c761111ea0e49bda6c4 to your computer and use it in GitHub Desktop.

Select an option

Save andlrc/c8e1a3b9c1ec5c761111ea0e49bda6c4 to your computer and use it in GitHub Desktop.
" dyngrepprg - set "grepprg" to dynamically when inside a git repository
" Maintainer: Andreas Louv <[email protected]>
" Date: 3 Aug 2018
augroup DynGrepPrg
au!
au DirChanged * call <SID>SetGrepPrg()
augroup END
function! s:SetGrepPrg()
if system('git rev-parse --show-toplevel 2> /dev/null') !~ '^\s*$'
set grepprg=git\ grep\ -n\ --column\ $*
else
set grepprg=grep\ -nH\ --exclude={\\*.o,tags}
\\ --exclude-dir={node_modules,.git}
\\ $*
endif
endfunction
call s:SetGrepPrg()
@Konfekt
Copy link

Konfekt commented Sep 15, 2025

Thank you! Below, an analogue for &findfunc

if executable('rg')
  let g:findcmd = 'rg --files --hidden --ignore-file='..expand('$XDG_CONFIG_HOME/ripgrep/ignore')..' --no-ignore --color never --glob ""'
else
  if has('win32')
      let g:findcmd = 'dir . /s/b/a:-d-h'
  elseif has('unix')
      let g:findcmd = 'find . -type f'
  endif
endif

let s:nul = ' 2> ' .. (has('win32') ? 'nul' : '/dev/null')
let g:findcmd = g:findcmd .. s:nul

" See :help findexpr
silent! func FindFiles(cmdarg, cmdcomplete)
  silent let fnames = systemlist(g:findcmd)
  return fnames->filter('v:val =~? a:cmdarg')
endfunc
set findfunc=FindFiles

if !executable('git') | finish | endif

let s:findcmd = g:findcmd
let s:git_ls = 'git ls-files . --exclude-from=' .. expand('$XDG_CONFIG_HOME/ripgrep/ignore') .. ' --exclude-standard --cached --others' .. s:nul
function! s:SetFindCmd()
  let g:findcmd =
        \ empty(trim(system('git rev-parse --show-toplevel' .. s:nul))) ?
        \ s:findcmd : s:git_ls
endfunction

autocmd vimrc DirChanged * call <SID>SetFindCmd()
doautocmd vimrc DirChanged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment