vim save and restore session

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()



Instead of saving the session to the folder where the file is opened, y ou can save the session file under the current working directory, here is the cod.e 
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 


The above may do some unexpected things on your behave, you can use the "Vim recipe" as below. . http://vim.runpaint.org/editing/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).


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值