Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (61 sloc) 3.52 KB
<h3 id="managing-sessions">Managing Sessions</h3>
<h4>Problem</h4>
<p>Every time you work on a project you need to manually open all of the files
it comprises, readjust the window size, etc. You'd like Vim to do all this
automatically.</p>
<h4>Solution</h4>
<p>Use <i>sessions</i>.</p>
<p>To save a session: <tt>:mksession</tt>. Vim saves the session information
as 'Session.vim' in the working directory; to specify your own filename
execute <tt>:mksession <var>file</var></tt> instead. To overwrite an existing
session follow the command with an exclamation mark: <tt>:mksession!</tt>.</p>
<p>To restore a session invoke Vim with the <tt>-S</tt> flag from the same
directory you saved <tt>Session.vim</tt> in: <tt>vim -S</tt>. If you used a
different filename for your session: <tt>vim -S <var>file</var></tt>. If
you're already inside Vim, you can load a session by sourcing the session
file, e.g. <tt>:source Session.vim</tt>.</p>
<h4>Discussion</h4>
<p>Applications such as <i>Mozilla Firefox</i> 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 <tt>$VIMHOME/Session.vim</tt>. You could add a
mapping something like this to your <a
href="#configuring-vim"><tt>vimrc</tt></a>.</p>
<pre><code>nmap SQ &lt;ESC&gt;:mksession! ~/vim/Session.vim&lt;CR&gt;:wqa&lt;CR&gt;</code></pre>
<p>(<kbd>SQ</kbd> for <i>S</i>ession <i>Q</i>uit). To automatically restore
this session when Vim is called without arguments add the following:</p>
<pre><code>function! RestoreSession()
if argc() == 0 "vim called without arguments
execute 'source ~/.vim/Session.vim'
end
endfunction
autocmd VimEnter * call RestoreSession()</code></pre>
<p>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. </p>
<p>If you don't just want one global session file, as described above, a more
granular approach is suggested below:</p>
<pre><code>nmap SSA :wa&lt;CR&gt;:mksession! ~/sessions/
nmap SO :wa&lt;CR&gt;:so ~/sessions/</code></pre>
<p><i>S</i>ession <i>S</i>ave <i>A</i>s saves the open files and prefills the
command line with the command to save the current session in a
<tt>~/sessions/</tt> directory. All you need to do is enter a name and hit
<kbd>&lt;Enter&gt;</kbd>.</p>
<p><i>S</i>ession <i>O</i>pen 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 <kbd>&lt;Enter&gt;</kbd>.</p>
<p>You can use <kbd>&lt;Tab&gt;</kbd> completion in both cases. For example, you
could save a session with <kbd>SSAwork&lt;Enter&gt;</kbd>. Later, when you
want to restore the session but can't recall its name, just hit
<kbd>SO&lt;Tab&gt;</kbd> to cycle through the saved sessions.</p>
<p>(Both mappings assume the <tt>~/sessions/</tt> directory already exists;
create it if it doesn't).</p>
<blockquote class="tip">
<p><i>Yuri Klubakov</i>'s <a
href="http://www.vim.org/scripts/script.php?script_id=2010"><tt>sessionman</tt></a>
<a href="#extending-vim-with-scripts-plugins">plugin</a> provides a more
polished approach for session management, subsuming the functionality
described above.</p>
</blockquote>
Jump to Line
Something went wrong with that request. Please try again.