11

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?

6
  • 1
    Have a look at :h mksession to start :)
    – nobe4
    Commented May 19, 2016 at 11:05
  • 6
    Probably a dupe of How to save window, split, and buffer layout? Commented May 19, 2016 at 12:13
  • @Carpetsmoker: You're right, I didn't remember the question existed.
    – statox
    Commented 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
    Commented 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
    Commented Feb 5, 2017 at 12:23

1 Answer 1

12

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 (e.g. :mksession! ~/.Session.vim)

You can also 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

Edit 7 years later

As mentioned in the comments it might be better not to use the VimEnter autocommand and trigger the sourcing manually to avoid sourcing the session in situations you don't want.

Or you could make the autocmd a bit smarted and source the session file only when you are in a specific directory.

Or you could have a shell alias like alias lvim='vim -S ~/Session.vim' to start a session only when you want to. (That's the solution I ended up using in the long term)

4
  • 1
    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. Commented Jan 24, 2020 at 20:08
  • 2
    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) Commented Apr 24, 2020 at 16:43
  • Yes, the directory needs updating. I had to change autocmd! VimLeave * mksession to autocmd! VimLeave * mksession! ~/Session.vim (note the ! for overwriting).
    – joeljpa
    Commented Nov 9, 2023 at 10:33
  • I however highly recommend not adding it to auto-load on VimEnter. It might have unpleasant side effects. Whenever I did a git commit, it opened my session instead of the commit message file! I've been using it fine just by manually loading it, like so vim -S ~/Session.vim.
    – joeljpa
    Commented Nov 9, 2023 at 12:21

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