Vim Tips Wiki

Git grep

Revision as of 09:21, April 25, 2008 by JohnBot (Talk | contribs)

1,619pages on
this wiki
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
  exe s
  let &grepprg = save
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'


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'))
nmap <C-x>G :call GitGrepWord()<CR>


Thanks for cleaning this tip. I have just converted it to use our TipImported template, and have used <pre>...</pre> for the code (more robust than a space indent in the long term).

I removed the "Possibly needs its own tip" html comment on GitGrepWord() because I see no reason to split this quite small tip. Also, we are holding discussions in plain view (like this comment) to make it easier for people trying to clean up the imported tips (see the discussion guidelines for why we don't use the talk page).

I think this tip should very briefly mention what git is (particularly since it's not a traditional tool).

FYI Ephemeral comments, such as this, will be deleted later, although we like to keep the ==Comments== section to encourage people to comment here rather than on the talk page.

--JohnBeckett 00:34, 8 December 2007 (UTC)

Around Wikia's network

Random Wiki