我正在尝试使用打开文件的相对路径在Vim中保存我的会话。使用cur_dir
中的sessionoptions
,文件的路径将是相对的。当前目录,但会话文件包含cd /path/to/base/directory
命令:
...
cd /path/to/base
badd +0 relpath1/file
badd +0 relpath2/file
...
如果我将curdir
从sessionoptions中删除,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结账到另一个。
如果没有为它设置包装函数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
我用一条额外的线修改了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
开头的线路,这将更好。