VIM将文件名的一部分替换为类似于ZSH cd的其他内容

VIM将文件名的一部分替换为类似于ZSH cd的其他内容,zsh,vim,Zsh,Vim,我最近改用了VIM。我在同一代码的多个分支上工作,因此经常会有两个文件具有非常相似的路径(例如:/home/user/turkey/code/helloworld.cpp),我希望将同一文件区分到另一个分支(例如:/home/user/kangaroo/code/helloworld.cpp) ZSH有cd命令,可以说cd turkey kangaroo来更改路径。我想在我的vimrc中找到/创建类似的东西(例如:diffsplitvert-turkey-kangaro)。有什么关于如何进行的建

我最近改用了VIM。我在同一代码的多个分支上工作,因此经常会有两个文件具有非常相似的路径(例如:/home/user/turkey/code/helloworld.cpp),我希望将同一文件区分到另一个分支(例如:/home/user/kangaroo/code/helloworld.cpp)

ZSH有cd命令,可以说
cd turkey kangaroo
来更改路径。我想在我的vimrc中找到/创建类似的东西(例如:
diffsplitvert-turkey-kangaro
)。有什么关于如何进行的建议吗


我知道当前文件的完整路径存储在
expand('%:p')
中,但我不确定如何更改此文件的内容。似乎vim replace仅限于编辑文件而不是寄存器。我希望在内存中执行此操作,而不是让它编辑文件。

在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>
将垂直拆分和差异:

/home/whatever/bar/code/foo.txt
它搜索完整的目录名,如
/foo/
/bar/
您可以更改
s:branchs
以满足您的需要。比如
let s:branchs=['/turkey/','/kangaroo/']


一个小的演示:

土耳其能在路径中多次显示吗?像
/home/usr/turkey/turkey(需要替换)/turkeyCode/foo.file
,分支是
/home/usr/turkey/kanaroo/turkeyCode/foo.file
?非常感谢Kent!这正是我所需要的!我改变了一些东西来自动生成目录,但很大程度上依赖于你的。这是我的最终结果:函数!ShowDiff()let filePath=expand('%:p')call inputsave()let newStream=input(“新流名称:”)call inputstore()let filePath=substitute(filePath,$USER.[a-zA-Z0-9]*,$USER.'.newStream'),如果文件可读(filePath)执行“vert diffsplit”。filePath else echo“其他分支的文件不存在于同一位置”endif endfunction nnoremap:call ShowDiff()
/home/whatever/bar/code/foo.txt