nvim-possession
No-nonsense session manager
You are puzzled by neovim sessions and are not using them, are you? Start your pos-sessions journey, fear no more!
This plugin is a no-nonsense session manager built on top of fzf-lua (required) that makes managing sessions quick and visually appealing: dynamically browse through your existing sessions, create new ones, update and delete with a statusline component to remind you of where you are. See for yourself:
๐ Installation and quickstart
Install nvim-possession
with your favourite plugin manager (fzf-lua
is required) and invoke require("nvim-possession").setup({})
; in order to avoid conflicts with your own keymaps we do not set any mappings but only expose the interfaces, which means you would need to define them yourself. The suggested quickstart configuration is, for instance
- with lazy.nvim
{
"gennaro-tedesco/nvim-possession",
dependencies = {
"ibhagwan/fzf-lua",
},
config = true,
init = function()
local possession = require("nvim-possession")
vim.keymap.set("n", "<leader>sl", function()
possession.list()
end)
vim.keymap.set("n", "<leader>sn", function()
possession.new()
end)
vim.keymap.set("n", "<leader>su", function()
possession.update()
end)
vim.keymap.set("n", "<leader>sd", function()
possession.delete()
end)
end,
}
Exposed interfaces
function | description | interaction |
---|---|---|
possession.list() | list all the existing sessions with fzf-lua; preview shows files in session | <CR> load selected session<Ctrl-x> delete selection session |
possession.new() | prompt for name to create new session | session folder must alredy exist, return a message error otherwise |
possession.update() | update current session (if new buffers are open) | do nothing if no session is loaded |
possession.delete() | delete current session (without prompt) | do nothing if no session is loaded |
๐ Usage and advanced configuration
As shown above the main use of the plugin is to show all existing sessions (say via <leader>sl
) and load the selected one upon <CR>
. Once a session is loaded a global variable is defined containing the session name (to display in a statusline - see below - or to validate which session is currently active). New sessions can also be created and updated on the fly, and they will show when you next invoke the list.
Default configurations can be found in the config and can be overriden at will by passing them to the setup({})
function: in particular the default location folder for sessions is vim.fn.stdpath("data") .. "/sessions/",
. You should not need to change any of the default settings, however if you really want to do so:
require("nvim-possession").setup({
sessions = {
sessions_path = ... -- folder to look for sessions, must be a valid existing path
sessions_variable = .. -- defines vim.g[sessions_variable] when a session is loaded
sessions_icon = ...
},
autoload = false, -- whether to autoload sessions in the cwd at startup
autosave = true, -- whether to autosave loaded sessions before quitting
autoswitch = {
enable = false -- whether to enable autoswitch
exclude_ft = {}, -- list of filetypes to exclude from autoswitch
}
save_hook = nil -- callback, function to execute before saving a session
-- useful to update or cleanup global variables for example
post_hook = nil -- callback, function to execute after loading a session
-- useful to restore file trees, file managers or terminals
-- function()
-- require('FTerm').open()
-- require('nvim-tree').toggle(false, true)
-- end
fzf_winopts = {
-- any valid fzf-lua winopts options, for instance
width = 0.5,
preview = {
vertical = "right:30%"
}
}
})
๐ช Automagic
If you want to automatically load sessions defined for the current working directory at startup, specify
require("nvim-possession").setup({
autoload = true -- default false
})
This autoloads sessions when starting neovim without file arguments (i. e. $ nvim
) and in case such sessions explicitly contain a reference to the current working directory (you must have vim.go.ssop+=curdir
); this is by design as this plugin intends to be as less invasive as possible.
Sessions are automatically saved before quitting, should buffers be added or removed to them. This defaults to true
(as it is generally expected behaviour), if you want to opt-out specify
require("nvim-possession").setup({
autosave = false -- default true
})
When switching between sessions it is often desirable to remove pending buffers belonging to the previous one, so that only buffers with the new session files are loaded. In order to achieve this behaviour specify
require("nvim-possession").setup({
autoswitch = {
enable = true, -- default false
}
})
this option autosaves the previous session and deletes all its buffers before switching to a new one. If there are some filetypes you want to always keep, you may indicate them in
require("nvim-possession").setup({
autoswitch = {
enable = true, -- default false
exclude_ft = {"...", "..."}, -- list of filetypes to exclude from deletion
}
})
A note on lazy loading: this plugin is extremely light weight and it generally loads in no time: practically speaking there should not be any need to lazy load it on events. If you are however opting in the autoload = true
feature, notice that by definition such a feature loads the existing session buffers in memory at start-up, thereby also triggering all other buffer related events (especially treesitter); this may result in higher start-up times but is independent of the plugin (you would get the same loading times by manually sourcing the session files).
Save-hook
Before saving a session, you can run actions that may update state or perform cleanup before updating the session.
For example, you may want to only save visible buffers to the session. This could be useful if loading a lot of buffers leads to slow startup. Or maybe you want to keep the tabline clean. To do so, you can use:
require("nvim-possession").setup({
save_hook = function()
-- Get visible buffers
local visible_buffers = {}
for _, win in ipairs(vim.api.nvim_list_wins()) do
visible_buffers[vim.api.nvim_win_get_buf(win)] = true
end
local buflist = vim.api.nvim_list_bufs()
for _, bufnr in ipairs(buflist) do
if visible_buffers[bufnr] == nil then -- Delete buffer if not visible
vim.cmd("bd " .. bufnr)
end
end
end
})
Post-hook
After loading a session you may want to specify additional actions to run that may not be have been saved in the session content: this is often the case for restoring file tree or file managers, or open up terminal windows or fuzzy finders or set specific options. To do so you can use
require("nvim-possession").setup({
post_hook = function()
require("FTerm").open()
require('nvim-tree').toggle(false, true)
vim.lsp.buf.format()
end
})
๐ฅ Statusline
You can call require("nvim-possession").status()
as component in your statusline, for example with lualine
you would have
lualine.setup({
sections = {
lualine_a = ...
lualine_b = ...
lualine_c = {
{ "filename", path = 1 },
{
require("nvim-possession").status,
cond = function()
return require("nvim-possession").status() ~= nil
end,
},
},
}
})
to display
the component automatically disappears or changes if you delete the current session or switch to another one.
Feedback
If you find this plugin useful consider awarding it a โญ, it is a great way to give feedback! Otherwise, any additional suggestions or merge request is warmly welcome!