Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Move categories to tip template)
Line 9: Line 9:
 
|version=5.7
 
|version=5.7
 
|rating=9/6
 
|rating=9/6
  +
|category1=
  +
|category2=
 
}}
 
}}
 
The following script will save your settings (see {{help|:mksession}}) on Vim exit, and load those settings when you enter Vim again.
 
The following script will save your settings (see {{help|:mksession}}) on Vim exit, and load those settings when you enter Vim again.

Revision as of 09:14, 25 April 2008

Tip 1202 Printable Monobook Previous Next

created April 10, 2006 · complexity basic · author inetic · version 5.7


The following script will save your settings (see :help :mksession) on Vim exit, and load those settings when you enter Vim again.

The settings are associated (by this script) to the directory from where you've started Vim. For example, if you start Vim from directory /home/mynick/project/foo, edit multiple buffers and then exit without closing the buffers, when you next run Vim from the same directory, all the buffers, mappings and other settings will be loaded back as they were when you left.

Just paste the following into your vimrc:

function! MakeSession()
  let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
  if (filewritable(b:sessiondir) != 2)
    exe 'silent !mkdir -p ' b:sessiondir
    redraw!
  endif
  let b:filename = b:sessiondir . '/session.vim'
  exe "mksession! " . b:filename
endfunction

function! LoadSession()
  let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
  let b:sessionfile = b:sessiondir . "/session.vim"
  if (filereadable(b:sessionfile))
    exe 'source ' b:sessionfile
  else
    echo "No session loaded."
  endif
endfunction
au VimEnter * :call LoadSession()
au VimLeave * :call MakeSession()

Without corrections this script will work only on Unix like systems.

Comments

Why not just use script#69?


Problem with editing crontab, /etc/passwd, and the like.

If you are in $HOME and edit the crontab via crontab -e and then quit Vim without first closing the buffer explicitly, and do this again from $HOME, you will look at an empty crontab.

Why? When you enter crontab -e, crontab essentially copies the current crontab to /tmp/<random_filename> and opens this temporary file in $EDITOR, which is of course Vim. The next time, crontab creates a new temporary file, but Vim restores the last session from $HOME. Hence, you end up editing an empty temporary file. What's more, if you save this file, it won't be activated afterwards because you made no changes to the current temporary crontab file.

The same goes for vipw and vigr and more utilities.

I think the tip would be more useful if it did only restore a session when Vim is started without a filename argument.


Yes, I agree, after a while I didn't like the behaviour of autoloading a session on VimEnter for similar reasons. I've changed line:

au VimEnter * :call LoadSession()

to:

map ,l :call LoadSession()<cr>

i.e. loading sesion on ",l" shortcut request.