Skip to content

qtyra/smart-persistence.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

(WIP DON'T USE IT) smart-persistence.nvim

smart-persistence.nvim is a small neovim plugin that uses the global working directory to save sessions.

Contents

Features

  • Sessions are loaded and saved based on the global working directory instead of the window/tab one.
  • Different sessions are saved based on the git branch.
  • Multiple sessions per directory and git branch.
  • Support auto session restore

Installation

Using lazy.nvim

{
    "qtyra/smart-persistence.nvim",
    opts = {}
},

Configuration

-- default settings, no need to copy
require("smart-persistence").setup({
    -- Where to save the sessions
    dir = vim.fn.stdpath("data") .. "/smart-persistence/",
    -- Don't automatically restore the session
    auto_restore = false,
    -- Don't automatically save or restore a session in these directories.
    excluded_dirs = { "~/Downloads" },
    -- smart-persistence.nvim will only auto save when there is at least one
    -- 'valid buffer', check out the 'valid_buffers' function in the code to
    -- understand what that means, excludes certain filetypes for being valid,
    -- especifically those listed here.
    excluded_filetypes = { "gitcommit", "gitrebase" },
    -- Maximum number of sessions stored per cwd and git branch.
    max_sessions = 10,
})

Usage

My recommended workflow is to have a dashboard plugin like dashboard dashboard.nvim or alpha-nvim and adding a one-key map to open the last session. Example with dashboard.nvim:

require("dashboard").setup({
    config = {
        center = {
            {
                action = 'lua require("smart-persistence").restore()',
                desc = " Restore Session",
                icon = "",
                key = "s",
            },
            -- other entries...
        },
    },
}

All exported functions:

-- Restore last session, set `auto_restore` to automatically call this function at startup.
vim.keymap.set("n", "<leader>qr", function() require("smart-persistence").restore() end)

-- Save the current session, it won't stop `auto_save`.
vim.keymap.set("n", "<leader>qv", function() require("smart-persistence").save() end)

-- Select a session based on your cwd and git branch.
vim.keymap.set("n", "<leader>qs", function() require("smart-persistence").select() end)

-- Don't save the current session
vim.keymap.set("n", "<leader>qS", function() require("smart-persistence").stop() end)

-- Remove all associated sessions with the current directory and git branch.
vim.keymap.set("n", "<leader>qd", function() require("smart-persistence").delete() end)

All exported commands:

  • :SmartPersistence restore
  • :SmartPersistence stop
  • :SmartPersistence select
  • :SmartPersistence delete
  • :SmartPersistence save

All events:

  • SmartPersistenceSavePre
  • SmartPersistenceSavePost
  • SmartPersistenceRestorePre
  • SmartPersistenceRestorePost
  • SmartPersistenceDeletePre
  • SmartPersistenceDeletePost

Alternatives

  • persistence.nvim:

    • Minimal wrapper around native vim sessions, it's your best option if you don't need all the bells and whistles of this or other plugins. The codebase is based on this plugin and i was the session plugin it was using before making my own.
    • Doesn't support auto restore, the author is not willing to add it
    • Doesn't use the global working directory.
  • auto-session:

    • Has a telescope extension, smart-persistence.nvim uses vim.ui.select so you can use dressing.nvim to use telescope (or fzf-lua as a selector).
    • Much more customizable, more options and more mature.
    • Doesn't support multiple sessions per directory.
    • The use of the global working directory was requested in 2022 but it's still open.