folke / persistence.nvim

💾 Simple session management for Neovim
Apache License 2.0
655 stars 25 forks source link

feature: auto load session on directory open #21

Closed aymanbagabas closed 1 year ago

aymanbagabas commented 1 year ago

Did you check the docs?

Is your feature request related to a problem? Please describe.

I would like to have a similar behavior to VSCode. When I open a project directory I would like the session to be loaded with all the previous buffers, options, and window sizes.

Describe the solution you'd like

Load session on directory open.

Describe alternatives you've considered

https://github.com/rmagatti/auto-session

Additional context

No response

2nthony commented 1 year ago

seems #13 and https://github.com/folke/persistence.nvim#-usage won't do auto

but we can write an auto command do this i think.

vim.api.nvim_create_autocmd('VimEnter', opts) -- not sure if is VimEnter event.
madmaxieee commented 1 year ago

you can do something like this

local persistenceGroup = vim.api.nvim_create_augroup("Persistence", { clear = true })
local home = vim.fn.expand "~"
local disabled_dirs = {
  home,
  home .. "/Downloads",
  "/private/tmp",
}

-- disable persistence for certain directories
vim.api.nvim_create_autocmd({ "VimEnter" }, {
  group = persistenceGroup,
  callback = function()
    local cwd = vim.fn.getcwd()
    for _, path in pairs(disabled_dirs) do
      if path == cwd then
        require("persistence").stop()
        return
      end
    end
    if vim.fn.argc() == 0 and not vim.g.started_with_stdin then
      require("persistence").load()
      require("nvim-tree.api").tree.toggle(false, true)
    else
      require("persistence").stop()
    end
  end,
  nested = true,
})
folke commented 1 year ago

sorry, not interested in adding this

rbhanot4739 commented 3 days ago

@madmaxieee Did it work for ? For me the session load does not happen when I open vim without any args although the autocmd is triggered. However if I do :=require("persistence").load() the session does gets loaded.

madmaxieee commented 3 days ago

@rbhanot4739 yes it works for me. for extra context, I set vim.g.started_with_stdin using this autocmd, which I forgot to include int the previous coomment:

-- disable persistence if nvim started with stdin
vim.api.nvim_create_autocmd({ "StdinReadPre" }, {
  group = persistence_group,
  callback = function()
    vim.g.started_with_stdin = true
  end,
})

hope that helps.