Skip to content

@joehillen /grep-with-git.vim
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
vim-grep-with-git
" 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.