A simple plugin for managing sessions in Neovim.
preview.mp4
- Create and load sessions
- Delete sessions
- Rename sessions
- List sessions (with telescope.nvim)
Using lazy.nvim:
local M = { "i0i-i0i/sessions.nvim" }
--- OPTIONAL (only for SessionsList) ---
M.dependencies = {
"nvim-telescope/telescope.nvim",
"nvim-lua/plenary.nvim"
}
--- OPTIONAL (only for SessionsList) ---
M.opts = {
path = "path/to/sessions_folder", -- default = "~/sessions"
attach_after_enter = true, -- if false just change cwd
promt_title = "Your title"
}
M.keys = {
{ "<leader>ss", "<cmd>SessionSave<cr>", desc = "Save session" },
{ "<leader>sc", "<cmd>SessionCreate<cr>", desc = "Create session" },
{ "<leader>sa", "<cmd>SessionAttach<cr>", desc = "Attach session" },
{ "<leader>sl", "<cmd>SessionsList<cr>", desc = "List sessions" }, -- only if you have telescope.nvim
}
return M
Create new session for current path:
vim.api.nvim_create_user_command("SessionCreate", function()
sessions.create_session()
end, {})
Show sessions list in telescope:
vim.api.nvim_create_user_command("SessionsList", function()
sessions.open_list()
end, {})
This creates a session but doesn't give it a name, you can use SessionAttach or SessionCreate to specify a name. Session is linked to the current directory:
vim.api.nvim_create_user_command("SessionSave", function()
sessions.save_session()
end, {})
Find session (in opts.path) for current path and attach, if not found print error:
vim.api.nvim_create_user_command("SessionAttach", function()
local _, err = pcall(sessions.attach_session)
if err then
print("Cann't found session here")
end
end, {})
Auto save session on exit and auto attach session on enter (you need to delete M.opts and use only M.config):
M.lazy = false -- REQUIRED
M.config = function()
local builtins = require("sessions").setup(opts)
vim.api.nvim_create_autocmd("VimEnter", {
callback = function()
vim.schedule(function()
builtins.attach()
end)
end
})
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
builtins.save()
end
})
end
Or you can move between previous session with <C-^>:
M.config = function()
---@class Session
---@field name string
---@field path string
---@type Session
local prev = { name = "", path = "" }
local builtins = require("sessions").setup()
---@param new_session Session
local goto_prev = function(new_session)
prev = builtins.get_current()
if new_session.path ~= "" and prev.path ~= new_session.path then
builtins.attach({ path = new_session.path })
end
end
vim.keymap.set("n", "<leader><C-^>", function()
goto_prev(prev)
end)
vim.api.nvim_create_user_command("CustomSessionAttach", function(input)
prev = builtins.get_current()
vim.cmd("SessionAttach " .. input.args)
end, { nargs = "?"})
end
M.keys {
-- ...
{ "<leader>sa", "<cmd>CustomSessionAttach<cr>", desc = "Attach session" }
}
{
["n"] = {
["dd"] = delete_session,
["rr"] = rename_session,
["<CR>"] = attach_session,
},
["i"] = {
["<C-d>"] = delete_session,
["<C-r>"] = rename_session,
["<CR>"] = attach_session,
}
}
- Passe to SessionAttach and sessions.attach_session() name of session
- sessions.get_current()
- <C-^> for sessions