persistence.nvim

repository·main·Indexed 21 days ago

https://github.com/folke/persistence.nvim

A Lua plugin for automated Neovim session management that saves active sessions to a local state directory on exit. It provides an API for loading and selecting sessions, as well as custom events like PersistenceLoadPre and PersistenceSavePost for integration with autocmds.

Tokens
724
Snippets
3
Records
4
Agent score
27%

What's inside persistence.nvim

  1. Install persistence.nvim with lazy.nvim

    main

    To install persistence.nvim using lazy.nvim, add the plugin to your configuration. It is recommended to use the BufReadPre event so that session saving only starts when an actual file is opened.

    -- Lua
    {
      "folke/persistence.nvim",
      event = "BufReadPre", -- this will only start session saving when an actual file was opened
      opts = {
        -- add any custom options here
      }
    }
  2. Configure persistence.nvim options

    main

    You can customize how sessions are stored and when they are saved using the opts table.

    Note: To control exactly which components (like buffers, globals, or marks) are included in the session file, configure Neovim's built-in 'sessionoptions' option.

    {
      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
    }
  3. Use the persistence.nvim API to manage sessions

    main

    The plugin provides a simple API to load, select, or stop session management. Use these functions to create keymaps for session handling.

    -- load the session for the current directory
    vim.keymap.set("n", "<leader>qs", function() require("persistence").load() end)
    
    -- select a session to load
    vim.keymap.set("n", "<leader>qS", function() require("persistence").select() end)
    
    -- load the last session
    vim.keymap.set("n", "<leader>ql", function() require("persistence").load({ last = true }) end)
    
    -- stop Persistence => session won't be saved on exit
    vim.keymap.set("n", "<leader>qd", function() require("persistence").stop() end)
  4. Use persistence.nvim events in autocmds

    main

    You can hook into the session lifecycle using the following events to run custom logic before or after loading/saving sessions:

    • PersistenceLoadPre: Triggered before loading a session.
    • PersistenceLoadPost: Triggered after loading a session.
    • PersistenceSavePre: Triggered before saving a session.
    • PersistenceSavePost: Triggered after saving a session.