normally when you work on a set of files, and you can open it up , edit them and later on you may need to close the vim and do something else, and later sometime you may need to reopen that in VIM , and you want to have the layout, the files editing to be opened again, even the register and clipboard.
You can do that via vim sessions.
you want something like
:mksession ~/mysession.vim
Then later you can source that vim file and you'll have your old session back:
:source ~/mysession.vim
or open vim with the -S option:
$ vim -S ~/mysession.vim
Addedum./Annex
There are more discussion that you can do autosaving by hooking to the VIM enter or exit event.One of configuration example is as follow.
fu! SaveSess()
execute 'call mkdir(%:p:h/.vim)'
execute 'mksession! %:p:h/.vim/session.vim'
endfunction
fu! RestoreSess()
execute 'so %:p:h/.vim/session.vim'
if bufexists(1)
for l in range(1, bufnr('$'))
if bufwinnr(l) == -1
exec 'sbuffer ' . l
endif
endfor
endif
endfunction
autocmd VimLeave * call SaveSess()
autocmd VimEnter * call RestoreSess()
fu! SaveSess()
execute 'mksession! ' . getcwd() . '/.session.vim'
endfunction
fu! RestoreSess()
if filereadable(getcwd() . '/.session.vim')
execute 'so ' . getcwd() . '/.session.vim'
if bufexists(1)
for l in range(1, bufnr('$'))
if bufwinnr(l) == -1
exec 'sbuffer ' . l
endif
endfor
endif
endif
syntax on
endfunction
autocmd VimLeave * call SaveSess()
autocmd VimEnter * call RestoreSess()
Annex/Affix
Managing Sessions
DISCUSSION
Applications such as Mozilla Firefox use the concept of a global session file which is overwritten every time you use the program. To make Vim work this way you simply use a fixed name for the session variable. For example, you could save it to$VIMHOME/Session.vim. You could add a mapping something like this to your vimrc.
nmap SQ <ESC>:mksession! ~/vim/Session.vim<CR>:wqa<CR>
(SQ for Session Quit). To automatically restore this session when Vim is called without arguments add the following:
function! RestoreSession()
if argc() == 0 "vim called without arguments
execute 'source ~/.vim/Session.vim'
end
endfunction
autocmd VimEnter * call RestoreSession()
You can extend this in arbitrary ways to suit your working environment. One approach is to only restore a session if it exists in the current file's directory. Another is to simply hardcode a list of directories whereby if they are the file's current directory or parent directory, their session file is used. This is useful for one-project-per-directory organisation.
If you don't just want one global session file, as described above, a more granular approach is suggested below:
nmap SSA :wa<CR>:mksession! ~/sessions/
nmap SO :wa<CR>:so ~/sessions/
Session Save As saves the open files and prefills the command line with the command to save the current session in a ~/sessions/ directory. All you need to do is enter a name and hit <Enter>.
Session Open also saves the open files, then prefills the command line with the command to load a session file. Just type the name of the session you want to load and hit <Enter>.
You can use <Tab> completion in both cases. For example, you could save a session with SSAwork<Enter>. Later, when you want to restore the session but can't recall its name, just hit SO<Tab> to cycle through the saved sessions.
(Both mappings assume the ~/sessions/ directory already exists; create it if it doesn't).