我最近改用 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>
然后按
<f5>
在正常模式下,将在 vert split 和 diff 模式下在相应的分支 (dir) 中打开相同的文件。例如现在你正在编辑
/home/whatever/foo/code/foo.txt
按
<F5>
将垂直拆分和差异:/home/whatever/bar/code/foo.txt
它搜索完整的目录名称,如
/foo/
或 /bar/
您可以更改 s:branches
以满足您的需求。喜欢 let s:branches=['/turkey/','/kangaroo/']
一个小demo:
关于zsh - VIM 用类似于 ZSH cd 的其他内容替换部分文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20849358/