If I'm viewing a multi-file diff in vim - for example the one produced by running :VCSDiff in a netrw buffer - and I position my cursor over a particular hunk, is there a way to jump to the affected code in another window, so I can get a better contextual view of what the diff is doing?

link|improve this question
feedback

1 Answer

I guess vim doesn't have this functionality built in, so I wrote a function in Python:

def DiffJump():
    """
    Based on the current position of the cursor, jump to the appropriate
    file/line combination.
    """
    row, col = vim.current.window.cursor
    buf = vim.current.window.buffer
    offt = -1
    havehunk = False
    for lnum in xrange(row-1, -1, -1):
        line = buf[lnum]
        if line.startswith("@@"):
            if havehunk:
                continue
            havehunk = True
            atat, minus, plus, atat = line.split()
            baseline = int(plus[1:].split(",")[0])
            realline = baseline + offt
        elif line.startswith("+++"):
            fname = line[4:].split("\t")[0]
            vim.command("e +{line:d} {fname}"
                        .format(line=realline, fname=fname))
            break
        elif not havehunk and line.startswith(" ") or line.startswith("+"):
            offt += 1
        elif line.startswith("-"):
            pass
    else:
        print "No hunk found."
link|improve this answer
doesn't just work with gf in normal mode when cursor is over the filename? – lambacck Sep 25 '11 at 4:18
ah, you want the line too. – lambacck Sep 25 '11 at 4:18
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.