Skip to content

Instantly share code, notes, and snippets.

Last active Jul 17, 2022
What would you like to do?
git-jump hack that can be used FROM Vim
usage() {
cat <<\EOF
usage: git jump <mode> [<args>]
Jump to interesting elements in an editor.
The <mode> parameter is one of:
diff: elements are diff hunks. Arguments are given to diff.
merge: elements are merge conflicts. Arguments are ignored.
grep: elements are grep hits. Arguments are given to git grep or, if
configured, to the command in `jump.grepCmd`.
ws: elements are whitespace errors. Arguments are given to diff --check.
open_editor() {
if test -t 1; then
editor=`git var GIT_EDITOR`
eval "$editor -q \$1"
eval "cat \$1"
mode_diff() {
git diff --no-prefix --relative "$@" |
perl -ne '
if (m{^\+\+\+ (.*)}) { $file = $1; next }
defined($file) or next;
if (m/^@@ .*?\+(\d+)/) { $line = $1; next }
defined($line) or next;
if (/^ /) { $line++; next }
if (/^[-+]\s*(.*)/) {
print "$file:$line: $1\n";
$line = undef;
mode_merge() {
git ls-files -u |
perl -pe 's/^.*?\t//' |
sort -u |
while IFS= read fn; do
grep -Hn '^<<<<<<<' "$fn"
# Grep -n generates nice quickfix-looking lines by itself,
# but let's clean up extra whitespace, so they look better if the
# editor shows them to us in the status bar.
mode_grep() {
cmd=$(git config jump.grepCmd)
test -n "$cmd" || cmd="git grep -n --column"
$cmd "$@" |
perl -pe '
s/[ \t]+/ /g;
s/^ *//;
mode_ws() {
git diff --check "$@"
if test $# -lt 1; then
usage >&2
exit 1
mode=$1; shift
trap 'rm -f "$tmp"' 0 1 2 3 15
tmp=`mktemp -t git-jump.XXXXXX` || exit 1
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
"mode_$mode" "$@" >"$tmp"
test -s "$tmp" || exit 0
open_editor "$tmp"


git-jump is an amazing little script that can be used to start Vim with the quickfix list populated with interesting things:

  1. The beginning of any diff hunks.
  2. The beginning of any merge conflict markers.
  3. Any grep matches, including the column of the first match on a line.
  4. Any whitespace errors detected by git diff --check.

Example usage:

$ git jump diff
$ git jump grep GetUser

But the original script is written from a shell perspective so it always opens Vim and thus can't be used to populate the quickfix list from within Vim. Bummer…

This hack makes it possible to get two behaviors out of git-jump for the price of one.

  • When executed in an interactive context, it starts Vim with the list:

    $ git jump diff
  • when executed in a non-interactive context, it outputs the list:

    $ git jump grep foo | grep -v bar


Having the same feature inside and outside of Vim is quite handy but:

:cexpr system('git jump diff')

is a lot to type! Let's turn that into a proper command:

:command! -bar -nargs=* Jump cexpr system('git jump ' . expand(<q-args>))

that we can use easily:

:Jump diff
:Jump merge
:Jump grep foo
Copy link

Gee19 commented Nov 29, 2021

@romainl Passing on some comments from the author of git-jump if you are interested:

If somebody was inclined to send a patch to the list that made
it work in that mode, I'd be happy to review it. From skimming what was
in that gist, I'd suggest:

  • rather than swapping the editor for "cat", the non-editor mode can
    just write straight to stdout, and avoid even the tempfile

  • it probably should be a command-line option, which gives people an
    escape hatch if they are doing something clever with stdout. I.e.,
    something like --no-editor, --editor, or --editor=auto (where the
    latter checks isatty(1), and could probably become the default).

Copy link

g0xA52A2A commented Nov 29, 2021

Sorry to spam Romain's notifications but I did try this some time ago [1], it was not a pleasant experience.


Copy link

romainl commented Nov 29, 2021

@Gee19 @george-b I actually planned to submit a patch (first patch to Git, you can imagine the excitation ;-)) but well… I couldn't even get past the email setup step so I simply gave up. Frankly, the contrib scripts are not even bundled with the Git package I use, so the benefit is not worth the effort for me and I'm happy with the script as it is.

If any of you want to take it further I would gladly support your effort. My initial implementation has been replaced by @george-b's anyway so I can't even claim paternity anymore. Hell, I don't even remember how or when I got the idea of that change in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment