Auto-saving sessions in Vim 10

Posted by ryan Tue, 20 Jul 2010 00:42:00 GMT

Back when I used Textmate, I liked that you could save projects to a file so that you could quit Textmate and come back later, load the project file up, and have all the files and tabs set up and open the way you had them. Since switching to Vim, I've gotten the same functionality (and more!) using the :mksession command. One thing that's missing in Vim, however, is the ability to auto-save a session. There are a few add-ons that do this, but add a bunch of other functionality that I find unnecessary. Below is a little script I wrote that adds auto-saving sessions to Vim:

function! AutosaveSessionOn(session_file_path)
  augroup AutosaveSession
    exec "au VimLeave * mks! " . a:session_file_path
  augroup end
  let g:AutosaveSessionFilePath = a:session_file_path

  echo "Auto-saving sessions to \"" . a:session_file_path . "\""

function! AutosaveSessionOff()
  if exists("g:AutosaveSessionFilePath")
    unlet g:AutosaveSessionFilePath

  augroup AutosaveSession
  augroup end
  augroup! AutosaveSession

  echo "Auto-saving sessions is off"

command! -complete=file -nargs=1 AutosaveSessionOn call AutosaveSessionOn(<f-args>)
command! AutosaveSessionOff call AutosaveSessionOff()

augroup AutosaveSession
  au SessionLoadPost * if exists("g:AutosaveSessionFilePath") != 0|call AutosaveSessionOn(g:AutosaveSessionFilePath)|endif
augroup end

To begin auto-saving sessions, simply run:

:AutosaveSessionOn <session filename>

Your session will then be automatically saved to the given session filename when Vim exits. Also, if you have globals in your sessionoptions list, when you load the auto-saved session, auto-saving will continue to occur to that session file. To turn auto-saving to the session file off, run:


For the latest and greatest version, refer to my vimrc.

  1. exchangerates about 1 month later:

    Thanks for sharing this. This is great and very helpful.

  2. high shoes outlet about 1 month later:

    "I do not care,"the 43-year-old shoemaker said,"I still love my job as before."“In Paris,I live in the original office,I still droved the Vespa motorcycle jimmy choo shoes.If you like to spend each day,you can see any change." Perhaps he failed to note that,since its first store in Paris,opened in 1992,outside interests.He did not christian louboutin,he is not also the magazine amateur.In fact he claimed that his design does not always seek inspiration from the fashion.

  3. reviews of provillus about 1 year later:

    it needs a bokmark so i can come back to it later ,nice stuff

  4. JCPenney over 1 year later:

    Thank you, this is really helpful.

  5. Color Hex over 1 year later:

    Great! I had exactly this problem yesterday. Thanks a lot!

  6. ghd straightner about 2 years later:

    Your blog is so beautiful and natural all are like your blog everyone appreciate your blog. Whenever We will see your blog every time we get something new about your blog This is the very informative content. I am very appreciate to your site who provide us this informative resource thank for sharing us this information.

  7. Suzanna over 2 years later:

    Hello there! Would you mind if I share your blog with my twitter group? There's a lot of people that I think would really appreciate your content. Please let me know. Thanks

  8. Karina over 2 years later:

    Howdy! Do you use Twitter? I'd like to follow you if that would be ok. I'm absolutely enjoying your blog and look forward to new updates.

  9. almost 3 years later:
  10. Asics Onitsuka Tiger almost 3 years later: