Vim Tips Wiki
Register
Advertisement
Tip 1262 Printable Monobook Previous Next

created June 18, 2006 · complexity basic · author Timo Hirvonen · version 7.0


git grep <pattern> searches for a pattern in a currently selected git branch. This adds :G <pattern> command to run the command from within Vim.

func GitGrep(...)
  let save = &grepprg
  set grepprg=git\ grep\ -n\ $*
  let s = 'grep'
  for i in a:000
    let s = s . ' ' . i
  endfor
  exe s
  let &grepprg = save
endfun
command -nargs=? G call GitGrep(<f-args>)

You can also limit searching to files matching a pattern (git will do the pattern matching):

:G <pattern> -- '*.c'

Additions[]

The following addition will run git grep on the word under the cursor when Ctrl+X G is pressed.

func GitGrepWord()
  normal! "zyiw
  call GitGrep('-w -e ', getreg('z'))
endf
nmap <C-x>G :call GitGrepWord()<CR>

Comments[]

Advertisement