Open cpakkala opened 5 years ago
Hey,
I agree this is a legit problem. I just checked, this happens in vim
as well. I'm surprised this didn't pop up before.
This plugin uses the equivalent of this to find CWD:
prefix
+ :
to enter tmux's command line mode.display -F "#{pane_current_path}"
to get CWD.In essence: we completely rely on tmux
to get CWD
. I think this is the right approach. With this thinking in mind, my first thought is this is actually an upstream (tmux's) problem and should be fixed there. I'm open to other arguments tho.
If we ever decide to fix the issue in this plugin, I'm not sure how that could be done.
I just tested this problem by changing vim's CWD with :lcd
- a window equivalent of :cd
. In this case tmux
will report different CWD if cursor is in a different vim window...
In case anyone else is having this issue, it's not as much of a problem if you make sure you have autochdir disabled in vim. Enabling that is what brought this issue to my attention.
I am having the same issue here, is there any workaround for it already?
have same issue: if I opening some files with vim
in different panes and in another pane I change directory and then save tmux session, after kill and restore tmux sesison, vim will not be restored successfully because the $cwd
has changed to new directory and vim will open with an empty buffer (in case there's no same file with same name).
Is there a way that we save current $cwd
of pane and when restore we cd
to that folder before restore the content?
I came up with this workaround. It requires [neo]vim to always use the directory of your Session.vim
as the current working directory, so consider it a compromise.
You can configure [neo]vim to persist sessions in a way where the directory containing the session file is used as the current working directory after restoring the session with -S
. As long as the current directory isn't changed again (see the end of this comment), tmux-resurrect will save the pane path in such a way that the session can be restored. If you aren't already saving your sessions in a file called Session.vim
, make sure to do that too.
In your init.vim
or .vimrc
:
set sessionoptions-=curdir
set sessionoptions+=sesdir
In addition to adjusting the option above, make sure that windows and tabs in existing sessions aren't setting the current working directory locally (i.e. with :lcd
or :tcd
). The following command will clear the local current working directories:
:tabdo windo cd ANY_PATH
Make sure to replace the last argument with any existing path.
Source $MYVIMRC
to update sessionoptions
and update your existing sessions with :mksession!
if necessary. For the latter, I recommend using vim-obsession if you aren't already.
Since changing the current directory affects how plugins and other commands work, you may want to look into adjusting other configuration. For example, I prefer not to keep my session files under the same directories as my projects, but I still want to be able to access project files quickly.
This is a very brief list, so feel free to suggest other recommendations and I'll add them here:
Make sure autochdir isn't on. This causes [neo]vim to change your current directory on a per-window basis:
" This should be 0. Unset the option if necessary.
:echo &autochdir
Make sure NERDTree doesn't change your current directory:
" This should be unset or 0
:echo g:NERDTreeChDirMode
Make sure ctrlp.vim uses your project root as a starting directory:
" This should be unset or any value except 0. See :help 'g:ctrlp_working_path_mode'.
:echo g:ctrlp_working_path_mode
For the longest time I ignored the annoying issue of my "nvim -S" command occasionally failing upon resurrection because the process was in the wrong directory (I'm using set-option -g @resurrect-strategy-nvim 'session'). I had assumed that resurrect was restoring the directory incorrectly. I checked my shell init files for cd commands and tested tmux new-window -c '/tmp' as suggested. I finally looked into it further and I see now that the currrent working directory (CWD) is being stored incorrectly (at least that's my view of it) in the ressurect txt file. I proved it by doing a save when my nvim process is foregrounded vs when it's backgrounded and diffed the resulting txt file:
Apparently, when nvim is in the foreground of the pane, tmux-resurrect is saving the CWD of the nvim process itself, not the shell which the process is running in; however, when I background nvim and save, the proper CWD of the shell is saved. This is a problem because we want the "nvim -S" to run in the directory where the Session.vim is stored, rather than where we may have done a temporary ":cd" command to within nvim.