auto-session
auto-session copied to clipboard
[FEATURE] Change save session filename after cwd changed
Is your feature request related to a problem? Please describe.
- open vim in directory A
- use
:cdto directory B (my be use in GUI nvim) - do some edit quit, trigger auto-save-session
- Use vim in directory A, but I open the files in dir B
Describe the solution you'd like
Use the autocmd DirChanged global to follow the cwd change. (Or add an option)
Describe alternatives you've considered
Here is my little workaround:
vim.cmd('autocmd DirChanged global lua require('auto-session-library').conf.last_loaded_session = nil')
The tricky part here is that DirChanged is after the cwd has already changed.
I'd need to know it's happening before it happens to be able to:
- Save the current session
- Grab the intended
cwd - Check if the new
cwdhas a corresponding session - If it does, load it, if it doesn't, do nothing.
Still thinking about a workaround for it.
PS: your current workaround is somewhat odd to me. It makes me think you're using the experimental last_loaded_session config, but I'm not quite sure how things are behaving with that lately. (haven't been using a GUI for a while now)
@rmagatti I have an idea. It seems that the problem is that there is no way to trigger an event before the cwd is changed. At VimEnter and at DirChanged we can save the cwd in a variable named "LastCWD" or something like that. Thus at DirChanged we always know the last cwd (before DirChanged). This is perhaps not the most elegant solution, but I assume last_loaded_session has to be used to take into account users saving a session with a custom path, and the same session later being autosaved (there might be more uses?). I would like to give it a go if no one else is currently working on this. Additionally this would be my first contribution to an open-source project :)
Just knowing what session existed before the :cd isn't enough. It's the ability to do work before it that matters more.
In the scenario you mention, what would have happened with the initial session before the :cd change? It would probably just not be saved at all.
My companion plugin rmagatti/session-lens actually solves this hopping to other sessions part, since it knows when a session (i.e a cwd) will change, it first saves the current session, then deletes all the buffers then restores the new session.
That same behavioiur can't really be achieved by doing :cd manually.
Yea, that's a good point - I missed that. An alternative solution would be to save the session every time a single tiny change is made (resize window etc.), but this seems very complicated and slow.
I actually just made a plugin called opener.nvim, which provides a :Open function, which is just :cd, but where buffers/windows/tabs are cleared as well. Also, you can setup hook-functions (just like in session-lens) to run before and after :Open is executed. I have tested it with auto-session and it gives a nice workflow, but I still had to set last_loaded_session to nil, like this:
require("opener").setup {
pre_open = {function()
vim.cmd "SaveSession"
end},
post_open = {"NERDTree", function(dir)
local lib = require "auto-session-library"
lib.conf.last_loaded_session = nil
vim.cmd "RestoreSession"
end},
}
Also, this of cause does not change the general problem, since people may still just change cwd using :cd or other commands.
I think maybe something that could help would be to allow disabling the use of conf.last_loaded_session or make the RestoreSesession/SaveSession functions prefer using the cwd to generate the session-filenames.
Ah yes. last_loaded_session was added to keep track of the current session as you can see in #45. It makes sense you'd need to set that back to nil.
Opened a vim/vim issue. Let's see if it ends up getting some tracktion or not. A DirChangedPre autocmd would make this problem trivial to solve.
Oh and congrats on your plugin 👏 @willthbill
Good news: https://github.com/vim/vim/issues/9721 is merged and is already in Neovim as well. Now I just need to find the time to implement this :)
Some LSP provides a feature to switch workspace which changed the based DIR inside a project automatically when opening a new file. But it may cause some issue when the session is reloaded as it will open a file with an incorrect path. e.g.
....
edit ~/github/nvim-treesitter-refactor/lua/nvim-treesitter-refactor.lua
...
edit nvim-treesitter-refactor.vim (this is in plugin dir)
Is there a way to add a hook to check if the file URI is correct or not? and only open the file when the path is valid.
Hey @ray-x I'm slightly confused by what you mean with checking if the file URI is correct. Do you mean in the resulting .vim file where the session is saved? Ultimately for this feature whenever the LSP changes the workspace, if that means changing cwds I'd imagine auto-session would just execute the same behaviour as session-lens and 1. save the current session 2. close all the buffers 3. load the new session for the new cwd. I have yet to implement and test this of course but that's somewhat how I imagine it working at first.
I think it is something wrong with neovim upstream. the session vim file should be able to replay and restore the last session. But from time to time it did not. Here is an example from a session file
cd ~/github/dotfiles/nvim
badd +133 lua/modules/lang/config.lua
argglobal
%argdel
$argadd lua/modules/lang/config.lua
edit lua/modules/lang/config.lua
argglobal
balt ~/github/dotfiles/nvim/modules/tools/plugins.lua
The issue is here
~/github/dotfiles/nvim/modules/tools/plugins.lua
The correct path should be ~/github/dotfiles/nvim/lua/modules/tools/plugins.lua
I saw it break from time to time and I suspect it related to dir changes. I normally use the telescope to fuzzy find and open files. Not sure if that is releavent.