我在 .vimrc 中有以下代码,可以在 vim 启动时自动保存/加载会话:
" Session saving
" Automatically save / rewrite the session when leaving Vim
augroup leave
autocmd VimLeave * mksession! ~/.vim/session.vim
augroup END
" Automatically silently load the session when entering vim
autocmd VimEnter * silent source ~/.vim/session.vim
哪个工作正常,我遇到的唯一问题是当我想创建新文件或打开现有文件时:
vim test.txt
在这种情况下,文件不会打开,而是加载了最后保存的会话。
所需的行为如下。当我跑步时vim
没有参数 - 它恢复上一个会话。如果我提供文件参数,例如vim test.py
- 它加载最后一个会话并在新选项卡中打开/创建提供的文件。
怎么做?理想情况下没有任何插件。
应该是这样的:
" use ++nested to allow automatic file type detection and such
autocmd VimEnter * ++nested call <SID>load_session()
function! s:load_session()
" save curdir and arglist for later
let l:cwd = getcwd()
let l:args = argv()
" source session
silent source ~/.vim/session.vim
"restore curdir (otherwise relative paths may change)
call chdir(l:cwd)
" open all args
for l:file in l:args
execute 'tabnew' l:file
endfor
" add args to our arglist just in case
execute 'argadd' join(l:args)
endfunction
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)