zsh - VIM 用类似于 ZSH cd 的其他内容替换部分文件名

标签 zsh vim

我最近改用 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:enter image description here

关于zsh - VIM 用类似于 ZSH cd 的其他内容替换部分文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20849358/

相关文章:

python - zsh:在 zsh 脚本中的heredoc之后转义换行符

regex - sed:替换多行

performance - 在 zsh 中的每个命令之后自动执行内置时间(如 tcsh 中的 `set time`)

vim - 有什么办法可以在 vim 中删除而不覆盖你上次的拉动吗?

vim - Vim 关闭时出现错误消息

bash - 如何将数据通过管道传输到交互式 bash 脚本并将输出通过管道传输到另一个命令?

macos - aws cli 自动缩放命令不会返回到 shell 提示符

pdf - 使用 vim 阅读 pdf 的内容

Vim - 奇怪的行为

vim - 映射 Alt + left|right 以移动选项卡