Saving session state


If you’ve ever needed to restart your computer unexpectedly, such as to update your system software or complete an installation, you might have been frustrated by having to restore Vim back to your working state. Vim has built-in session management that can save your state in these cases. Think of it as the “Restore all tabs” of Vim. Here’s how it works:

When you need to exit Vim but want to save your state, run the :mksession command, which can be shortened to :mks:

  • :mks some_file.vim – Save your current session state to some_file.vim inside the current working directory.

By default, the file will go in the current working directory. I myself save these in something like tmp/something.vim to keep them out of the way and out of version control. You could put them anywhere on your system though. If you reuse the same session file, you’ll need to use :mks! to overwrite your existing.

Once you save your session, exit Vim and go about your business. When you’re ready to pick up where you left off:

vim -S tmp/something.vim

Or, if you’re in Vim already, you can source the session:

  • :source tmp/something.vim – Source a session file and restore session

Vim will open up all your windows, tabs, splits, buffers, even settings you have :set will be just as you left it. You won’t have marks or registers, but I haven’t really missed those in my usage of Vim sessions.

How useful was this tip?

Average rating 4.2 / 5. Vote count: 73

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

92 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

3 thoughts on “Saving session state”

  1. I love sessions but unfortunately they don’t always play well with other vim components. For example, they don’t save tab names, and i got the taboo plugin to fix that. Nerdtree for example also doesn’t store the current directory with sessions, and I now use dirvish for session support alone

  2. Live vim sessions! I used to treat them more as a curiosity until I learned you can pair this functionality with scp to make a session that automatically connects to a remote server.

Leave a reply

Your email address will not be published. Required fields are marked *