start Vim and restore a specific session, you can use the
following command: >
vim -S vimbook.vim
This tells Vim to read a specific file on startup. The 'S' stands for
session (actually, you can source any Vim script with -S, thus it might as
well stand for "source").
The windows that were open are restored, with the same position and size as
before. Mappings and option values are like before.
What exactly is restored depends on the 'sessionoptions' option. The
default value is:
"blank,buffers,curdir,folds,help,options,tabpages,winsize,terminal".
blank keep empty windows
buffers all buffers, not only the ones in a window
curdir the current directory
folds folds, also manually created ones
help the help window
options all options and mappings
tabpages all tab pages
winsize window sizes
terminal include terminal windows
Change this to your liking. To also restore the size of the Vim window, for
example, use: >
:set sessionoptions+=resize
SESSION HERE, SESSION THERE
The obvious way to use sessions is when working on different projects.
Suppose you store your session files in the directory "~/.config/nvim". You
are currently working on the "secret" project and have to switch to the
"boring" project: >
:wall
:mksession! ~/.config/nvim/secret.vim
:source ~/.config/nvim/boring.vim
This first uses ":wall" to write all modified files. Then the current session
is saved, using ":mksession!". This overwrites the previous session. The
next time you load the secret session you can continue where you were at this
point. And finally you load the new "boring" session.
If you open help windows, split and close various windows, and generally mess
up the window layout, you can go back to the last saved session: >
:source ~/.config/nvim/boring.vim
Thus you have complete control over whether you want to continue next time
where you are now, by saving the current setup in a session, or keep the
session file as a starting point.
Another way of using sessions is to create a window layout that you like to
use, and save this in a session. Then you can go back to this layout whenever
you want.
For example, this is a nice layout to use:
>
+----------------------------------------+
| VIM - main help file |
| |
|Move around: Use the cursor keys, or "h|
|help.txt================================|
|explorer | |
|dir |~ |
|dir |~ |
|file |~ |
|file |~ |
|file |~ |
|file |~ |
|~/=========|[No File]===================|
| |
+----------------------------------------+
<
This has a help window at the top, so that you can read this text. The narrow
vertical window on the left contains a file explorer. This is a Vim plugin
that lists the contents of a directory. You can select files to edit there.
More about this in the next chapter.
Create this from a just started Vim with: >
:help
CTRL-W w
:vertical split ~/
You can resize the windows a bit to your liking. Then save the session with:
>
:mksession ~/.config/nvim/mine.vim
Now you can start Vim with this layout: >
vim -S ~/.config/nvim/mine.vim
Hint: To open a file you see listed in the explorer window in the empty
window, move the cursor to the filename and press "O". Double clicking with
the mouse will also do this.
SESSIONS AND SHADA
Sessions store many things, but not the position of marks, contents of
registers and the command line history. You need to use the shada feature
for these things.
In most situations you will want to use sessions separately from shada.
This can be used to switch to another session,