跳转到vim中当前补丁大块的文件/行


2

如果我在vim中查看多文件差异 - 例如通过运行生成的差异 :VCSDiff 在一个 netrw 缓冲区 - 我将光标放在特定的块上,有没有办法在另一个窗口中跳转到受影响的代码,这样我就可以更好地了解差异在做什么?

Answers:


3

我猜vim没有内置的这个功能,所以我在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."

当光标在文件名上时,它不仅仅在正常模式下使用gf?
lambacck

啊,你也想要这条线。
lambacck
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.