Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: restore last session on startup #58

Open
1 task done
yashamon opened this issue May 27, 2024 · 1 comment
Open
1 task done

feature request: restore last session on startup #58

yashamon opened this issue May 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@yashamon
Copy link

Did you check existing requests?

  • 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 yashamon added the enhancement New feature or request label May 27, 2024
@scottmckendry
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants