Check also fugitive.vim - A Git wrapper so awesome, it should be illegal
(that is its official tag-line ;) ), from Tim Pope (see the Git repo project):
It includes, amongst many other commands, one for a git grep
:
call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Ggrep :execute s:Grep(<bang>0,<q-args>)")
call s:command("-bar -bang -nargs=* -complete=customlist,s:EditComplete Glog :execute s:Log('grep<bang>',<f-args>)")
function! s:Grep(bang,arg) abort
let grepprg = &grepprg
let grepformat = &grepformat
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
try
execute cd.'`=s:repo().tree()`'
let &grepprg = s:repo().git_command('--no-pager', 'grep', '-n')
let &grepformat = '%f:%l:%m'
exe 'grep! '.a:arg
let list = getqflist()
for entry in list
if bufname(entry.bufnr) =~ ':'
let entry.filename = s:repo().translate(bufname(entry.bufnr))
unlet! entry.bufnr
elseif a:arg =~# '\%(^\| \)--cached\>'
let entry.filename = s:repo().translate(':0:'.bufname(entry.bufnr))
unlet! entry.bufnr
endif
endfor
call setqflist(list,'r')
if !a:bang && !empty(list)
return 'cfirst'
else
return ''
endif
finally
let &grepprg = grepprg
let &grepformat = grepformat
execute cd.'`=dir`'
endtry
endfunction