我最近改用VIM.我在相同代码的多个分支上工作,因此通常两个文件将具有非常相似的路径(例如:/home/user/turkey/code/helloworld.cpp),我想在另一个分支中区分相同的文件(例如:/家庭/用户/袋鼠/代码/ helloworld.cpp).
ZSH有cd命令,可以说cd turkey kangaroo改变路径.我想在我的vimrc中找到/创建类似的东西(例如:) diffsplit vert turkey kangaroo.关于如何解决这个问题的任何建议?
我知道存储当前文件的完整路径expand('%:p')但不确定如何更改其内容.似乎vim替换仅限于编辑文件而不是寄存器.我想在内存中执行此操作,而不是编辑文件.
在你的 vimrc 中添加以下代码:
let s:branches=['/foo/','/bar/']
function! ShowDiff()
let other = expand('%:p')
let do_diff = 0
if match(other, s:branches[0])>0
let other = substitute(other, s:branches[0], s:branches[1],'')
let do_diff = 1
elseif match(other, s:branches[1])>0
let other = substitute(other, s:branches[1], s:branches[0],'')
let do_diff = 1
endif
if do_diff
exec 'vert diffsplit '. other
endif
endfunction
nnoremap <F5> :call ShowDiff()<cr>
Run Code Online (Sandbox Code Playgroud)
然后<f5>在正常模式下按下将在垂直分割和差异模式下打开相应分支(目录)中的相同文件。
例如现在你正在编辑
/home/whatever/foo/code/foo.txt
Run Code Online (Sandbox Code Playgroud)
按下<F5>将进行垂直分割和比较:
/home/whatever/bar/code/foo.txt
Run Code Online (Sandbox Code Playgroud)
它会搜索完整的目录名称,例如/foo/,或者/bar/您可以更改s:branches以满足您的需要。喜欢let s:branches=['/turkey/','/kangaroo/']
一个小演示: