I want to save a list of open files (:ls), quit gvim开发者_运维技巧, start gvim, reopen all previously opened files. I don't want to use :mksession because it doesn't work correctly (probably due to some of the plugins I'm using)
Maybe the bug/conflict with :mksession is only if you have parts of :mksession enabled that you don't care about.
Try this:
:set sessionoptions=buffers
:mksession
Another answer suggests session manager plugins, which are great. But I have a very lightweight alternative that I'll leave here for posterity. (Since it has been quite a while since this question was active.)
The code below automatically saves the current session on shutdown, and sets a key command to reload it manually (handy for when I really do want a new session with new files). If you really do want to unconditionally restore the previous session whenever vim is started up again, uncomment the 'VimEnter' line.
As a bonus, this also enables manually saving and restoring a separate session at will with a keypress.
Somewhere in ~/.vim/vimrc
" Automatically save the current session whenever vim is closed
autocmd VimLeave * mksession! ~/.vim/shutdown_session.vim
" <F7> restores that 'shutdown session'
noremap <F7> :source ~/.vim/shutdown_session.vim<CR>
" If you really want to, this next line should restore the shutdown session
" automatically, whenever you start vim. (Commented out for now, in case
" somebody just copy/pastes this whole block)
"
" autocmd VimEnter source ~/.vim/shutdown_session.vim<CR>
" manually save a session with <F5>
noremap <F5> :mksession! ~/.vim/manual_session.vim<cr>
" recall the manually saved session with <F6>
noremap <F6> :source ~/.vim/manual_session.vim<cr>
The user can define for herself what is in a session with the 'sessionoptions' option, mentioned above: help sessionoptions
There are a couple plugins that take vim's session management a little bit further.
SessionMan and Autosess provide some commands and auto saving features that you might like.
Another one is the following: http://jaredforsyth.com/blog/2010/apr/9/vim-crash-recovery/
Very short, probably a vimrc kind of thing.
I've found setting viminfo in your .vimrc can also save the last known buffer list. Here's what I have.
"Set viminfo to save info upon exit
"help usr_21.txt
"'5 : marks will be remembered up to 10 previous files
""50 : will save up to 100 lines for each register
":20 : up to 20 lines of command-line history will be remembered
"% : saves and restores the buffer list
set viminfo='5,f1,\"50,:20,%,n~/.vim/viminfo
精彩评论