如果我在 vim 中查看多文件差异(例如,通过:VCSDiff
在缓冲区中运行产生的差异netrw
),并且将光标放在特定块上,是否有办法跳转到另一个窗口中受影响的代码,这样我就可以更好地了解差异正在做什么?
答案1
我猜 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."