Say I have a couple of .py files open in vim.
How do I ensure that the previous setup will be opened as it is every time I start vim(Just like VS code).


The other answer recommends using sessions, which is correct, but I’m not a huge fan of blindly always creating them and restoring them. Sometimes I don’t want that, and then this would get in my way.

Instead, I recommend one of two approaches:

  1. Manually run :mksession when you want one; or,
  2. Install tpope’s Obsession plugin, and tun :Obsession.

With either one, you can jump into the session with vim -S sessionfile.vim. Since both the vim commands and the -S option default to the name Session.vim, there’s very little typing (vim -S is usually enough).

The big things Obsession provides are

  • only storing sane things in the session, and
  • automatically keeping the session file up-to-date

Found 3 answers-
1) Place these in your ~/.vimrc to write and load sessions with F2, F3

map <F2> :mksession! ~/vim_session <cr> " Quick write session with F2
map <F3> :source ~/vim_session <cr>     " And load session with F3


2) Put this in your ~/.vimrc to automate the process

" Automatically save the session when leaving Vim
autocmd! VimLeave * mksession
" Automatically load the session when entering vim
autocmd! VimEnter * source ~/Session.vim

2nd(though this requires ~/Session.vim)

3)More information 3rd

  • 2
    Best would be to put info from the links (such as for 3) directly in the post as well. – D. Ben Knoble Feb 16 '20 at 14:59

Currently I use the following approach which I find both compact and flexible:

augroup vimStartup | au!
    " ... other stuff here
    " save session on exit (:h v:this_session)
    autocmd VimLeavePre *
        \   if !empty(v:this_session)
        \ |     execute 'mksession!' v:this_session
        \ | endif
augroup end

So if using vim -S or nvim "+so ~/Session.vim" to start then the session will be both loaded and saved. Otherwise nothing will happen. "To start tracking" session manually simply use :so ~/Session.vim or :mks! anytime. Then "stop tracking" session with :let v:this_session = '' etc.

So far I found no use in installing an extra plugin for session management.

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