Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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."
share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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