代码之家  ›  专栏  ›  技术社区  ›  wasd

当会话自动加载时如何在vim中打开文件?

  •  0
  • wasd  · 技术社区  · 6 年前

    " 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 没有参数-它恢复上一个会话。如果我提供文件参数,e.x。 vim test.py 怎么做?理想情况下没有任何插件。

    0 回复  |  直到 6 年前
        1
  •  3
  •   Matt    6 年前

    " 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