Persistence is a simple lua plugin for automated session management.
- automatically saves the active session under
~/.local/state/nvim/sessions
on exit - simple API to restore the current or last session
- Neovim >= 0.7.2
Install the plugin with your preferred package manager:
-- Lua
{
"folke/persistence.nvim",
event = "BufReadPre", -- this will only start session saving when an actual file was opened
opts = {
-- add any custom options here
}
}
Persistence comes with the following defaults:
{
dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
pre_save = nil, -- a function to call before saving the session
post_save = nil, -- a function to call after saving the session
save_empty = false, -- don't save if there are no open file buffers
pre_load = nil, -- a function to call before loading the session
post_load = nil, -- a function to call after loading the session
}
Persistence works well with plugins like startify
or dashboard
. It will never restore a session automatically,
but you can of course write an autocmd that does exactly that if you want.
-- restore the session for the current directory
vim.api.nvim_set_keymap("n", "<leader>qs", [[<cmd>lua require("persistence").load()<cr>]], {})
-- restore the last session
vim.api.nvim_set_keymap("n", "<leader>ql", [[<cmd>lua require("persistence").load({ last = true })<cr>]], {})
-- stop Persistence => session won't be saved on exit
vim.api.nvim_set_keymap("n", "<leader>qd", [[<cmd>lua require("persistence").stop()<cr>]], {})