Editors like Sublime Text remember your session, so that whenever you open Sublime Text it is exactly how it was when you last closed it:

  • All of the same files are open
  • Your cursor is in the same position
  • Sublime Text even remembers changes to unsaved files

Can I get the same behavior in vim?

  • 1
    Have a look at :h mksession to start :)
    – nobe4
    May 19, 2016 at 11:05
  • 6
    Probably a dupe of How to save window, split, and buffer layout? May 19, 2016 at 12:13
  • @Carpetsmoker: You're right, I didn't remember the question existed.
    – statox
    May 19, 2016 at 12:19
  • 1
    Looks like that question covers the first two points, but I'm yet to find anything that covers the last one (remember changes to unsaved files)
    – Justin
    May 19, 2016 at 12:42
  • Help: :h 'undofile' and :h undo-persistence. You might want to read the following section of vim-galore as well: github.com/mhinz/…
    – lsrdg
    Feb 5, 2017 at 12:23

1 Answer 1


Vim has a built-in session mechanism, from :h session:

A Session keeps the Views for all windows, plus the global settings. You can save a Session and when you restore it later the window layout looks the same. You can use a Session to quickly switch between different projects, automatically loading the files you were last working on in that project.

It should fill your 2 first requirements but the third one Sublime Text even remembers changes to unsaved files isn't supported out of the box.

To save your sessions you need to use :mksession (:h :mksession). By default the command will save the file in ~/Session.vim but you can provide a file path as argument or modify the default behavior with sessionoptions (:h 'sessionoptions').

To reload your session use :source ~/Session.vim (or whatever filepath you used with :mksession).

Note that you need Vim to be compiled with the +mksession feature to use the sessions.

Finally to automate the process you can add this to your vimrc:

" Automatically save the session when leaving Vim
autocmd! VimLeave * mksession
" Automatically load the session when entering vim
autocmd! VimEnter * source ~/Session.vim
  • got to be careful adding this to vimrc as a) you probably won't always open vim from ~, and b) if Session.vim doesn't exist, this will cause error. Jan 24, 2020 at 20:08
  • Your answer is not entirely correct, probably because you changed your ~/.vimrc a long time ago: Session files are stored in ./Session.vim by default (at least that was the default setting in my case and I didn't change any settings) Apr 24, 2020 at 16:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.