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.4 / 5. Vote count: 96

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

105 Posts

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