Neovim Session Manager
A Neovim 0.7+ plugin that use built-in :mksession
to manage sessions like folders in VSCode. It allows you to save the current folder as a session to open it later. The plugin can also automatically load the last session on startup, save the current one on exit and switch between session folders.
The plugin saves the sessions in the specified folder (see configuration). The session corresponds to the working directory. If a session already exists for the current folder, it will be overwritten.
Dependencies
Commands
Use the command :SessionManager[!]
with one of the following arguments:
Argument | Description |
---|
load_session | Select and load session. (Your current session won't appear on the list). |
load_last_session | Removes all buffers and tries to :source the last saved session. Returns true if the session was restored and false otherwise. |
load_current_dir_session | Removes all buffers and tries to :source the last saved session of the current directory. Returns true if the session was restored and false otherwise. |
load_git_session | When in a git repo, removes all buffers and tries to :source the last saved session of the git repo root directory. Returns true if the session was restored and false otherwise. |
save_current_session | Works like :mksession , but saves/creates current directory as a session in sessions_dir . |
delete_session | Select and delete session. |
delete_current_dir_session | Deletes the session associated with the current directory. |
When !
is specified, the modified buffers will not be saved.
Commands load_session
and delete_session
use vim.ui.select()
. To use your favorite picker like Telescope, consider installing dressing.nvim or telescope-ui-select.nvim.
Configuration
To configure the plugin, you can call require('session_manager').setup(values)
, where values
is a dictionary with the parameters you want to override. Here are the defaults:
local Path = require('plenary.path')
local config = require('session_manager.config')
require('session_manager').setup({
sessions_dir = Path:new(vim.fn.stdpath('data'), 'sessions'),
session_filename_to_dir = session_filename_to_dir,
dir_to_session_filename = dir_to_session_filename,
autoload_mode = config.AutoloadMode.LastSession,
autosave_last_session = true,
autosave_ignore_not_normal = true,
autosave_ignore_dirs = {},
autosave_ignore_filetypes = {
'gitcommit',
'gitrebase',
},
autosave_ignore_buftypes = {},
autosave_only_in_session = false,
max_path_length = 80,
})
Autoload mode
If Neovim is started without arguments the value of the autoload_mode option is used to determine which session to initially load. The following modes are supported:
Mode | Description |
---|
Disabled | No session will be loaded. |
CurrentDir | The session in the current working directory will be loaded. |
LastSession | The last session will be loaded. This is the default. |
GitSession | If in a git repo the session for repository root will be loaded |
autoload_mode
can be set to either a single mode or an array of modes, in which
case each mode will be tried until one succeeds e.g.
autoload_mode = { config.AutoloadMode.CurrentDir, config.AutoloadMode.LastSession }
Would attempt to load the current directory session and then fallback to the last session.
Autocommands
You can specify commands to be executed automatically after saving or loading a session using the following events:
Event | Description |
---|
SessionSavePre | Executed before a session is saved |
SessionSavePost | Executed after a session is saved |
SessionLoadPre | Executed before a session is loaded |
SessionLoadPost | Executed after a session is loaded |
For example, if you would like to have NvimTree or any other file tree automatically opened after a session load, have this somewhere in your config file:
local config_group = vim.api.nvim_create_augroup('MyConfigGroup', {})
vim.api.nvim_create_autocmd({ 'User' }, {
pattern = "SessionLoadPost",
group = config_group,
callback = function()
require('nvim-tree.api').tree.toggle(false, true)
end,
})
Save session on BufWrite
You can enable this opt in feature with
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
callback = function ()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_get_option_value("buftype", { buf = buf }) == 'nofile' then
return
end
end
session_manager.save_current_session()
end
})
For more information about autocmd and its event, see also: