Skip to content

Instantly share code, notes, and snippets.

@xolox
Created March 15, 2011 10:11
Show Gist options
  • Save xolox/870543 to your computer and use it in GitHub Desktop.
Save xolox/870543 to your computer and use it in GitHub Desktop.
Vim command to search version controlled directory trees using grep -F
" While getting to know new code bases I frequently "grep" for identifiers
" because Exuberant Ctags and Cscope only on work for a few programming
" languages while "grep" works on all text files.
command! -nargs=+ -complete=customlist,xolox#complete#keywords SearchRepo call s:SearchRepo(<f-args>)
function! s:SearchRepo(...)
cclose
let repository = finddir('.git', '.;')
if repository == ''
let repository = finddir('.hg', '.;')
endif
if repository == ''
call xolox#warning("No repository found!")
else
" XXX The first fnamemodify() call sometimes makes {repository} end in a
" slash. The second fnamemodify() will then happily strip the slash and
" not do anything useful. Am I misunderstanding something?! :-S
let repository = fnamemodify(repository, ':p')
let repository = substitute(repository, '/*$', '', '')
let directory = fnamemodify(repository, ':h')
execute 'cd' fnameescape(directory)
call xolox#message("Searching %s", directory)
let command = 'grep -IRF ' . shellescape(join(a:000, "\n")) . ' *'
execute 'silent' command
cwindow
execute 'cd' fnameescape(directory)
call setqflist(getqflist(), 'r')
call xolox#message("Matched %i occurrences", len(getqflist()))
if exists(':MultiMatch')
" Highlight matching occurrences.
execute 'windo MultiMatch!' join(a:000, ' ')
endif
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment