public
Created

vim-grep-with-git

  • Download Gist
grep-with-git.vim
VimL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
" This is the grep command I've always wanted.
" This plugin opens the results of 'grep -r' in a bottom window
" and uses 'git grep' when in a git repo and regular grep otherwise.
 
" <C-x><C-x> runs grep for the word under the cursor
" :G <word> runs grep
" :Gi <word> runs grep as case-insensitive
 
function! Grep(args, ignorecase)
let grepprg_bak=&grepprg
let g:gitroot=system('git rev-parse --show-cdup')
if v:shell_error
if a:ignorecase
let g:mygrepprg="grep\\ -nir"
else
let g:mygrepprg="grep\\ -nr"
endif
let g:grepcmd="silent! grep " . a:args . " ."
else
if a:ignorecase
let g:mygrepprg="git\\ grep\\ -ni"
else
let g:mygrepprg="git\\ grep\\ -n"
endif
let g:grepcmd="silent! grep " . a:args . g:gitroot
endif
 
exec "set grepprg=" . g:mygrepprg
execute g:grepcmd
botright copen
let &grepprg=grepprg_bak
exec "redraw!"
endfunction
 
func GrepWord()
normal! "zyiw
call Grep(getreg('z'), 0)
endf
 
nmap <C-x><C-x> :call GrepWord()<CR>
command! -nargs=1 G call Grep( '<args>', 0)
command! -nargs=1 Gi call Grep( '<args>', 1)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.