stevearc / resession.nvim

A replacement for mksession with a better API
MIT License
174 stars 13 forks source link

feature request: restore last session on startup #58

Open yashamon opened 1 month ago

yashamon commented 1 month ago

Did you check existing requests?

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.

scottmckendry commented 1 week ago

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,
}