resession.nvim icon indicating copy to clipboard operation
resession.nvim copied to clipboard

feature request: restore last session on startup

Open yashamon opened this issue 1 year ago • 1 comments

Did you check existing requests?

  • [X] I have searched the existing issues

Describe the feature

I can't find a way to configure to restore last session on startup.

Provide background

No response

What is the significance of this feature?

nice to have

Additional details

It would be good to have the following behavior: if no running neovim instance exists, then restore last session on startup. If an instance does exist, then just open neovim without loading any sessions.

Alternatively, always restore the session that was last terminated.

yashamon avatar May 27 '24 14:05 yashamon

Try the following:

{
    "stevearc/resession.nvim",
    lazy = false,
    config = function()
        local resession = require("resession")
        resession.setup({})
        vim.api.nvim_create_autocmd("VimEnter", {
            callback = function()
                -- Only load the session if nvim was started with no args
                if vim.fn.argc(-1) == 0 then
                    -- Open the last session if it exists
                    resession.load("last", { silence_errors = true })
                end
            end,
            nested = true,
        })
        vim.api.nvim_create_autocmd("VimLeavePre", {
            callback = function()
                -- Always save a special session named "last"
                resession.save("last", { notify = false })
            end,
        })
    end,
}

This config saves the current session on exit as "last". When you open nvim, it will attempt to restore a session with the same name.

If you want separate sessions per director, you can change it to the following:

{
    "stevearc/resession.nvim",
    lazy = false,
    config = function()
        local resession = require("resession")
        resession.setup({})
        vim.api.nvim_create_autocmd("VimEnter", {
            callback = function()
                -- Only load the session if nvim was started with no args
                if vim.fn.argc(-1) == 0 then
                    -- Save these to a different directory, so our manual sessions don't get polluted
                    resession.load(vim.fn.getcwd(), { dir = "dirsession", silence_errors = true })
                end
            end,
            nested = true,
        })
        vim.api.nvim_create_autocmd("VimLeavePre", {
            callback = function()
                resession.save(vim.fn.getcwd(), { dir = "dirsession", notify = true })
            end,
        })
    end,
}

scottmckendry avatar Jun 20 '24 07:06 scottmckendry