Introduction to Vim Sessions

Vim supports sessions, but in a very lightweight kind of way. When the :mksession {file} (:help :mksession, abbreviation: :mks) command is issued, Vim will save certain settings. You can safely try this out right now -- just type :mksession ~/session, then open the file and take a look inside.

The first line should be let SessionLoad = 1. This causes the SessionLoad variable to be set when the session file is loaded, allowing Vim to track when a session file is loaded. To load a session file, all you need to do is source it with the :source command (:help :source, abbreviation: :so). I think it's cool that there's no specific "load session" command -- :source just reads Ex commands from a file.

Another thing worth learning about sessions is you can control what :mksession includes by editing the 'sessionoptions' (:help 'sessionoptions', abbreviation: 'ssop') variable. Mine currently looks like this: blank,buffers,curdir,folds,help,options,tabpages,winsize. You can view yours by typing :set ssop?. The help page for this setting lists each supported option -- one that I haven't got set is winpos which records the position of the Vim window (I'm using console Vim).

There are a lot of popular plugins for enhancing Vim's session support. One that I picked at random is vim-session by Peter Odding, which can automatically save sessions and even saves NERD Tree windows.

blog comments powered by Disqus