Save settings between Vim sessions

From Vim Tips Wiki

Jump to: navigation, search
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)
    return
  endif
  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)
  endif
endfunction

function! SessExtraFname()
  if v:this_session==""
    call confirm("There is no loaded session","&Ok",1)
    return ''
  endif
  return substitute(v:this_session,'\.[^.]*$','x.vim','')
endfunction
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.

[edit] Comments

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
    autocmd!
    let fn=SessExtraFname()
    if has("win32")
      let fn=substitute(fn,'\\\ze\S','/','g')
    endif
    execute "autocmd BufLeave ".fn." source ".fn
  augroup END
endfunction

Personal tools