【问题标题】:Force Vim's mksession to use relative paths?强制 Vim 的 mksession 使用相对路径?
【发布时间】:2012-11-10 14:29:56
【问题描述】:

我正在尝试使用打开文件的相对路径将我的会话保存在 Vim 中。在sessionoptions 中使用cur_dir,文件的路径将是相对的。当前目录,但会话文件包含cd /path/to/base/directory 命令:

...
cd /path/to/base
badd +0 relpath1/file
badd +0 relpath2/file
...

如果我将curdir 排除在会话选项之外,cd 命令就会消失,但文件路径将是绝对路径:

badd +0 /path/to/base/relpath1/file
badd +0 /path/to/base/relpath2/file

有没有办法让只有相对路径wrt。到创建会话时的当前目录 - 没有插件或编写脚本?这样会话文件就只有:

badd +0 relpath1/file
badd +0 relpath2/file

我的最终目标是拥有一个可以复制的会话文件,例如从 SVN 结帐到另一个。

【问题讨论】:

    标签: session vim relative-path absolute-path


    【解决方案1】:

    如果不为其设置包装函数,AFAIK,您将无法做到这一点。

    例如类似:

    function! MakeSession()
      let b:sessiondir = getcwd()
      let b:filename = b:sessiondir . '/session.vim'
      exe "mksession! " . b:filename
      exe "edit! " . b:filename
      exe "g:^cd :d"
      exe "x" 
    endfunction
    

    【讨论】:

    • 你能解释一下g:^cd :d吗?我仍然有使它工作的问题。我发现这条线很关键,但不知道它做了什么。谢谢。
    • 嗨@JohnChain,当然。这是一个ex 模式命令,它在与行首cd 的正则表达式匹配的每一行上全局执行行删除(d)。
    【解决方案2】:

    我用额外的一行修改了 Botykai 的答案,以全局删除绝对路径。

    function! MakeSession()
      let b:sessiondir = getcwd()
      let b:filename = b:sessiondir . '/_vimsession'
      exe "mksession! " . b:filename
      exe "edit! " . b:filename
      " Delete the line start with 'cd ...'
      exe "g:^cd :d"
      " Vim complains about b:sessiondir being undefined. So I use getcwd() directly
      " exe "%s:" . b:sessiondir . "::g". Use ':' to avoid path escape
      exe "%s:" . getcwd() . "/::g"
      " Save with 'x'
      exe "x"
    endfunction
    

    如果有人可以改进上述功能,将行缩小到仅以badd 开头的行,那就更好了。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我使用以下受Zsolt Botykai 的解决方案和John Chain 的解决方案启发的函数来解决它。 另外,我定义了一个宏来用更少的按键来执行这个功能。

      function! MakeSession() 
          let cwd = getcwd()
          let filename = cwd . '/.vim'
          exe "mksession! " . filename
          exe "tabedit! " . filename
          exe "silent g:^cd :d"
          exe "silent g:^lcd :d"
          "exe "silent %s:\V" . cwd . "/::ge"
          " ^ Don’t work because getcwd() expand the ~ while mksession does not !
          exe "silent %s?\\v \\~=/.+/? ?g"
          " ^ backslash need to be protected
          exe "x"
      endfunction
      
      nnoremap <leader>mks :call MakeSession()<cr>
      

      主要区别是删除完整路径的正则表达式。 之所以需要它是因为getcwd 扩展了主目录的~,但mksession 没有(在Mac OS 上)。

      【讨论】:

        【解决方案4】:

        使用curdir 而不是sesdir(请参阅:help sessionoptions)时解决方案更简单,因为绝对路径在会话文件中仅出现一次(cd path)。因此,MakeSession 函数更小(我将会话文件称为.vim):

        function! MakeSession()
            exe "mksession! .vim"
            exe "tabedit! .vim"
            exe "silent g:^cd :d"
            exe "x"
        endfunction
        
        nnoremap <leader>mks :call MakeSession()<cr>
        

        选择会话文件的名称(执行::call MakeNamedSession('foo')):

        function! MakeNamedSession(arg)
            let radical = a:arg
            exe "mksession! " . radical . ".vim"
            exe "tabedit! " . radical . ".vim"
            exe "silent g:^cd :d"
            exe "x"
        endfunction
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多