Manual handling
The :mksession! command records in a Session.vim in the current directory details about the current session.
You can then start a new vim instance using vim -S to reload the session. In case you forgot to use the -S flag you can subsequently restore the session using :source Session.vim
Vim Obsession and other plugins such as "rmagatti/auto-session" provides automatically saving of the session and restoring. Those plugins, including also the popular Obsession` support suspending session tracking.
Session Options
The sessionoptions option (:help sessionoption) specify what will be recorded when a session is saved. By default:
- The buffer list is recorded including hidden buffers, names are preserved but numbers are not
- Windows are recreated preserving the layout and size of splits
- Tab pages are restored in the same order
Neovim adds another mechanism, the shada file :help shada-file where the command-line history, search history, content of registory and location marks are recorded. This feature is enabled by default
Undos
Undos are not persisted , so if you type u or U (to undo all the changes in the last file) after you closed and re-opened Neovim this won’t be very useful. Plugins such as kevinhwang91/nvim-fundo helps saving a “forever” history, while telescope-undo provides fuzzy matching
- neovim Telescope undo video
Restoring Terminal commands
If you launch a terminal with :terminal and then you restore the window, Neovim is going to restore a shell session. If you instead launch a terminal command using
:terminal npm start dev upon restart, Neovim will restart npm.
You can see the problem after launching a terminal and then run npm inside it using ls
=> :ls
<= 1 a "app.js" line 1
2 #a "test/app-test.js" line 1
3 a- "term://.//78008:/usr/local/bin/bash" line 0
4 %a- "term://.//78095:/usr/local/bin/bash" line 18
However, thanks to the command :file {name} this can be fixed. Navigate to the buffer, then run :file term://PORT=3001 npm run server and then you can see from
=> :ls
<= 1 a "app.js" line 1
2 #a "test/app-test.js" line 1
3 a- "term://PORT=3001 npm run server" line 0
4 %a- "term://PORT=3002 npm run test:watch" line 18
So existing via :mksession followed by :qa! and restarting with nvim -S will restore the terminals