10

This question already has an answer here:

I just learned about tabs, meaning I can open them via

:tabe some/file
:tabe yet/another/file
:tabe foo

and circle them via gt (activate right one) and gT (activate left one).

Yet when I close my vim instance via quit-all :qa, all my tabs are gone. How can I restore them all when entering vim again?

marked as duplicate by EvergreenTree, Martin Tournoij Nov 28 '15 at 4:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

10

In vim, there is a feature which allows you to save your current session to a vimscript file. This can be done using the :mksession command. Here is a synopsis of the command given in the vim documentation:

:mks[ession][!] [file]
Write a Vim script that restores the current editing session.
When [!] is included an existing file is overwritten.
When [file] is omitted "Session.vim" is used.

You can then load a saved session by running vim -S Session.vim. The -S option will take the file given to it and source it before vim starts. (see :h :source) Also, if you omit the file name from the -S option, it will try to source a file in the current directory called Session.vim. (only if -S is given as the last argument and the file exists)

There is an option which allows you to specify what is saved in a session file as well, called 'sessionoptions'. This option is a comma separated list of words, with each word enabling the saving or restoring of something via the :mksession command. Here is a list of the possible words in this option and what is saved for each one:

  • blank - empty windows
  • buffers - hidden and unloaded buffers, not just those in windows
  • curdir - the current directory
  • folds - manually created folds, opened/closed folds and local fold options
  • globals - global variables that start with an uppercase letter and contain at least one lowercase letter. Only String and Number types are stored.
  • help - the help window
  • localoptions - options and mappings local to a window or buffer (not global values for local options)
  • options - all options and mappings (also global values for local options)
  • resize - size of the Vim window: 'lines' and 'columns'
  • sesdir - the directory in which the session file is located will become the current directory (useful with projects accessed over a network from different systems)
  • slash - backslashes in file names replaced with forward slashes
  • tabpages - all tab pages; without this only the current tab page is restored, so that you can make a session for each tab page separately
  • unix - with Unix end-of-line format (single ), even when on Windows or DOS
  • winpos - position of the whole Vim window
  • winsize - window sizes

As an example, the default value for this option would be the following:

sessionoptions=blank,buffers,curdir,folds,help,options,tabpages,winsize

For more on these topics, see :help :mksession and :help 'sessionoptions'.

  • nice breakdown! whenever I restore a session I lose the NERDTree window - which is not important but out of interest - do you know if its possible to restore NERDTree using the sessionoptions you have described? – the_velour_fog Nov 27 '15 at 18:09
  • I don't know if there is a way of restoring a Nerdtree window since I don't use that plugin, but it might be because of something special that it does with its buffer s – EvergreenTree Nov 27 '15 at 20:18
  • Is there a recommended way of automating this? I don't always have the opportunity to manually create a session before vim is closed. Manually re-opening a dozen+ tabs each time I return to the machine more than negates the time advantage vim has of being "lightweight" versus a full-fledged IDE startup process that remembers these sorts of things automatically. – Alex Jansen Feb 28 '17 at 19:53
  • It's not quite fully automatic, but tpope's "obsession" plugin works nicely for me: github.com/tpope/vim-obsession – EvergreenTree Apr 5 '18 at 13:07
5

You can save a session of vim with all its settings including your open tabs with

:mksession ~/session.vim

and load it with

:source ~/session.vim

if you don't want to store options like your colorscheme and font size you can disable storing them with this entry in your vimrc

set sessionoptions-=options

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