Created
October 15, 2012 20:39
-
-
Save mstroeck/3895285 to your computer and use it in GitHub Desktop.
vim-grep-with-git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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