Here's a useful shell procedure:

vigg ()
  git grep --color=auto -lz "$1" | xargs -r0 sh -c "vi +/\"$1\" \"\$@\" < /dev/tty"

git grep is a very effective way to run a recursive grep over a git repository, or part of a git repository (by default, it limits its search to the sub-tree you are currently sitting in). I quite often find myself wanting to edit every file that matched a search, and so wrote this snippet.

The /dev/tty-ugliness is to work around vi complaining that it's standard input was set to /dev/null by xargs. BSD xargs has a command, -o, which sorts this out; GNU xargs doesn't, but the manual suggests the above portable workaround. This marks the first time a BSD tool has had a feature I've wanted and the GNU equivalent doesn't.


comment 1

From the git-grep manpage:

  -O [<pager>], --open-files-in-pager [<pager>]
     Open the matching files in the pager (not the output of grep). 
     If the pager happens to be "less" or "vi", and the user specified only 
     one pattern, the first file
     is positioned at the first match automatically.

Doesn't do this do the same as your script?

Comment by christoph,
comment 1

jupp $(git grep -l "$1")

Fails with whitespace in filenames, but then, nobody does that anyway. And is easy enough to type inline, without needing a function.

Comment by mirabilos,
comment 1
Upstream Git includes the contrib script git-jump which provides this function (git jump grep) and more (diff, merge). The only difference is that it will open a quickfix list, so you'll have to use :cn instead of :bn.
Comment by Mischa POSLAWSKY,