Vim Tips Wiki

Save settings between Vim sessions

Revision as of 09:34, September 29, 2008 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
1,614pages on
this wiki
Tip 863 Printable Monobook Previous Next

created January 31, 2005 · complexity basic · author Ivan Tishchenko · version 6.0

I often use vimsession-files to save set of options, buffers, etc, that are specific for each job (see :help :mksession). To save some additional settings, which are not saved in vimsession (like commands, autocommands, functions), I use vimsession-extra file. When I want to modify that extra-file, I had to type something like :new <c-r>=v:this_session<CR>, then edit commandline to get proper extra-file name and edit it. It was tiring. So I wrote this plugin:

command! -bar SessSave call SessSave()
function! SessSave()
  if v:this_session==""
    call confirm("There is no loaded session","&Ok",1)
  if 2==confirm('Save session '.v:this_session.'?',"&No\n&Yes",1)
    exe 'mksession! '.v:this_session
    call confirm('Session '.v:this_session.' saved.','&Ok',1)

function! SessExtraFname()
  if v:this_session==""
    call confirm("There is no loaded session","&Ok",1)
    return ''
  return substitute(v:this_session,'\.[^.]*$','x.vim','')
command! -bar SessExtraOpen exe 'new '.SessExtraFname()
command! -bar SessExtraReread exe 'source '.SessExtraFname()

I also put following mapping there:

nmap <c-z>l :SessSave<CR>

Of course, you can change lhs to whatever you want, or add some extra mappings, for example for SessExtraOpen or SessExtraReread.


See also VimTip238.

Taking in account that wonderful VimTip879 we may change definition of SessExtraOpen so that it sets up autocommand which will reload our extra-file each time we modify it:

command! -bar SessExtraOpen call SessExtraOpen()
function! SessExtraOpen()
  exe 'new '.SessExtraFname()
  augroup SessAuGroup
    let fn=SessExtraFname()
    if has("win32")
      let fn=substitute(fn,'\\\ze\S','/','g')
    execute "autocmd BufLeave ".fn." source ".fn
  augroup END

Around Wikia's network

Random Wiki