### Manual Setup for persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Manually sets up the persistence.nvim plugin by calling its setup function with custom options. This is an alternative to configuring it via a package manager's `opts` table. ```lua -- Manual setup (if not using lazy.nvim opts) require("persistence").setup({ dir = vim.fn.stdpath("state") .. "/sessions/", need = 1, branch = true, }) ``` -------------------------------- ### Install persistence.nvim with lazy.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Installs the persistence.nvim plugin using the lazy.nvim package manager. It configures the plugin to start session saving only when an actual file is opened and sets options for the session directory, minimum buffer requirements, and git branch awareness. ```lua -- lazy.nvim installation { "folke/persistence.nvim", event = "BufReadPre", -- only start session saving when an actual file was opened opts = { dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved need = 1, -- minimum number of file buffers needed to save (0 = always save) branch = true, -- use git branch to save session } } ``` -------------------------------- ### Install persistence.nvim with Lazy.nvim Source: https://github.com/folke/persistence.nvim/blob/main/doc/persistence.nvim.txt This code snippet shows how to install the persistence.nvim plugin using the Lazy.nvim package manager. It specifies the plugin and an event to trigger its loading, along with a placeholder for custom options. ```lua { "folke/persistence.nvim", event = "BufReadPre", -- this will only start session saving when an actual file was opened opts = { -- add any custom options here } } ``` -------------------------------- ### Usage Examples for persistence.nvim API Source: https://github.com/folke/persistence.nvim/blob/main/doc/persistence.nvim.txt This Lua code demonstrates how to use the persistence.nvim API to manage sessions. It includes keybindings for loading the current or last session, selecting a session to load, and stopping persistence to prevent saving on exit. ```lua -- load the session for the current directory vim.keymap.set("n", "qs", function() require("persistence").load() end) -- select a session to load vim.keymap.set("n", "qS", function() require("persistence").select() end) -- load the last session vim.keymap.set("n", "ql", function() require("persistence").load({ last = true }) end) -- stop Persistence => session won't be saved on exit vim.keymap.set("n", "qd", function() require("persistence").stop() end) ``` -------------------------------- ### Start Automatic Session Saving with persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Re-enables automatic session saving after it has been stopped using `require('persistence').stop()`. This function sets up the necessary autocmd to trigger session saving on exit. It is called automatically during the plugin's initial setup. ```lua -- Re-enable session saving after stopping require("persistence").start() -- Check if persistence is active before toggling if not require("persistence").active() then require("persistence").start() end ``` -------------------------------- ### Load Session with persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Loads a Neovim session using the persistence.nvim plugin. It can load the session for the current working directory or the most recently used session by passing `{ last = true }`. Examples include direct calls and keybindings. ```lua -- Load session for current directory require("persistence").load() -- Load the last used session (most recently modified) require("persistence").load({ last = true }) -- Keybinding example vim.keymap.set("n", "qs", function() require("persistence").load() end, { desc = "Restore session for current directory" }) vim.keymap.set("n", "ql", function() require("persistence").load({ last = true }) end, { desc = "Restore last session" }) ``` -------------------------------- ### Get Last Session Path - persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Retrieves the file path of the most recently modified session. This is useful for quickly accessing or verifying the last active session. ```lua -- Get the most recent session path local last_session = require("persistence").last() if last_session then print("Last session: " .. last_session) end ``` -------------------------------- ### Get Current Session Path - persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Obtains the session file path for the current working directory. It can optionally include the git branch in the filename based on configuration. Passing `{ branch = false }` retrieves the path without the branch information. ```lua -- Get current session path (with branch if configured) local session_path = require("persistence").current() print("Current session would be saved to: " .. session_path) -- Get session path without branch local base_session = require("persistence").current({ branch = false }) print("Base session path: " .. base_session) ``` -------------------------------- ### Get Git Branch Name - persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Fetches the current git branch name. It returns nil if the current directory is not a git repository or if the git command execution fails. This function is primarily used internally for creating branch-specific session files. ```lua -- Get current git branch local branch = require("persistence").branch() if branch then print("Current branch: " .. branch) else print("Not in a git repository") end ``` -------------------------------- ### Configure Keymaps for Persistence Functions - persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Sets up comprehensive keybindings for all persistence functions within a single configuration block. This includes loading, selecting, and managing sessions, with optional integration for which-key.nvim for discoverability. ```lua -- Complete keybindings setup local persistence = require("persistence") vim.keymap.set("n", "qs", persistence.load, { desc = "Load session for cwd" }) vim.keymap.set("n", "qS", persistence.select, { desc = "Select session" }) vim.keymap.set("n", "ql", function() persistence.load({ last = true }) end, { desc = "Load last session" }) vim.keymap.set("n", "qd", persistence.stop, { desc = "Stop session saving" }) vim.keymap.set("n", "qr", persistence.start, { desc = "Resume session saving" }) vim.keymap.set("n", "qw", persistence.save, { desc = "Save session now" }) -- Which-key integration (if using which-key.nvim) local wk = require("which-key") wk.register({ q = { name = "Session", s = { persistence.load, "Load session" }, S = { persistence.select, "Select session" }, l = { function() persistence.load({ last = true }) end, "Load last session" }, d = { persistence.stop, "Don't save session" }, r = { persistence.start, "Resume saving" }, w = { persistence.save, "Save session" }, }, }, { prefix = "" }) ``` -------------------------------- ### Default Configuration for persistence.nvim Source: https://github.com/folke/persistence.nvim/blob/main/doc/persistence.nvim.txt This snippet outlines the default configuration options for persistence.nvim. It includes the directory for saving sessions, a minimum buffer count to trigger saving, and a boolean to control git branch usage for session saving. ```lua { dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved -- minimum number of file buffers that need to be open to save -- Set to 0 to always save need = 1, branch = true, -- use git branch to save session } ``` -------------------------------- ### Select Session with persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Opens an interactive picker to select and load a saved Neovim session. It uses `vim.ui.select` to display available sessions, showing their directory paths. Selecting a session changes the current directory and loads the chosen session. ```lua -- Open session picker require("persistence").select() -- Keybinding example vim.keymap.set("n", "qS", function() require("persistence").select() end, { desc = "Select a session to load" }) ``` -------------------------------- ### Persistence Lifecycle Events - persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Allows listening to lifecycle events from persistence.nvim using Neovim's User autocmd pattern. Supported events include PersistenceLoadPre, PersistenceLoadPost, PersistenceSavePre, and PersistenceSavePost, enabling custom actions at different stages of session management. ```lua -- Run code before a session is loaded vim.api.nvim_create_autocmd("User", { pattern = "PersistenceLoadPre", callback = function() -- Close all buffers before loading session vim.cmd("silent! %bdelete") end, }) -- Run code after a session is loaded vim.api.nvim_create_autocmd("User", { pattern = "PersistenceLoadPost", callback = function() vim.notify("Session loaded!") end, }) -- Run code before saving a session vim.api.nvim_create_autocmd("User", { pattern = "PersistenceSavePre", callback = function() -- Close certain buffer types before saving for _, buf in ipairs(vim.api.nvim_list_bufs()) do if vim.bo[buf].buftype == "terminal" then vim.api.nvim_buf_delete(buf, { force = true }) end end end, }) -- Run code after saving a session vim.api.nvim_create_autocmd("User", { pattern = "PersistenceSavePost", callback = function() vim.notify("Session saved!") end, }) ``` -------------------------------- ### List Saved Sessions with persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Retrieves a list of all saved session files, sorted by modification time with the most recent first. The function returns an array containing the full file paths of each session. This can be used to display or process saved sessions. ```lua -- Get all sessions sorted by most recent local sessions = require("persistence").list() -- Print session count and paths print("Found " .. #sessions .. " sessions:") for i, session in ipairs(sessions) do print(i .. ": " .. session) end ``` -------------------------------- ### Manually Save Session with persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Manually saves the current Neovim session to disk using the persistence.nvim plugin. The session file is named based on the current working directory and optionally the git branch. Saved sessions are Vim script files that can be sourced. ```lua -- Manually save current session require("persistence").save() -- Keybinding example vim.keymap.set("n", "qw", function() require("persistence").save() end, { desc = "Save current session" }) ``` -------------------------------- ### Stop Automatic Session Saving with persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Stops persistence.nvim from automatically saving the session when Neovim exits. This is useful for discarding the current session state or preventing an existing session from being overwritten. It can be re-enabled with `require('persistence').start()`. ```lua -- Stop session from being saved on exit require("persistence").stop() -- Keybinding example vim.keymap.set("n", "qd", function() require("persistence").stop() end, { desc = "Don't save current session" }) ``` -------------------------------- ### Check if Session Saving is Active with persistence.nvim Source: https://context7.com/folke/persistence.nvim/llms.txt Checks whether automatic session saving is currently active in persistence.nvim. It returns `true` if sessions will be saved on exit, and `false` if saving has been stopped. This function is useful for conditional logic, such as toggling the feature. ```lua -- Check if session saving is active local is_active = require("persistence").active() -- Toggle session saving vim.keymap.set("n", "qt", function() local persistence = require("persistence") if persistence.active() then persistence.stop() vim.notify("Session saving disabled") else persistence.start() vim.notify("Session saving enabled") end end, { desc = "Toggle session saving" }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.