tmux-plugins / tmux-resurrect

Persists tmux environment across system restarts.
MIT License
11.17k stars 418 forks source link

$CWD is taken from vim rather than shell #277

Open cpakkala opened 5 years ago

cpakkala commented 5 years ago

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:

$ diff prev last
34c34
< pane  maintenance     1       :tmux-cfg       1       :*      1       :/home/username/.tmux    1       zsh     :nvim -S
---
> pane  maintenance     1       :tmux-cfg       1       :*      1       :/home/username/bin      0       nvim    :nvim -S

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.

bruno- commented 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:

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.

bruno- commented 5 years ago

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...

cpakkala commented 5 years ago

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.

zkalves commented 5 years ago

I am having the same issue here, is there any workaround for it already?

tmpm697 commented 5 years ago

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?

adgoudz commented 4 years ago

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

For existing sessions

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.

Working around the workaround

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: