VIM用类似于ZSH cd的其他东西替换部分文件名

时间:2013-12-30 23:34:22

标签: zsh vim

我最近改用VIM。我在相同代码的多个分支上工作,因此通常两个文件将具有非常相似的路径(例如:/home/user/turkey/code/helloworld.cpp),我想在另一个分支中区分相同的文件(例如:/家/用户/袋鼠/代码/ helloworld.cpp)。

ZSH有cd命令,可以说cd turkey kangaroo来改变路径。我想在我的vimrc中找到/创建类似的东西(例如:diffsplit vert turkey kangaroo)。关于如何解决这个问题的任何建议?

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

1 个答案:

答案 0 :(得分:1)

在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>将在垂直分割和差异模式下打开相应分支(dir)中的相同文件。

e.g。现在你正在编辑

/home/whatever/foo/code/foo.txt

<F5>将进行vert-split和diff:

/home/whatever/bar/code/foo.txt

它会搜索完整的目录名称,例如/foo//bar/,您可以更改s:branches以符合您的需求。比如let s:branches=['/turkey/','/kangaroo/']

一个小型演示:enter image description here

相关问题