tmux-plugins / tmux-resurrect

Persists tmux environment across system restarts.
MIT License
11.42k stars 424 forks source link

Detect in bashrc when a tmux-resurrect shell is being started? #368

Open himat opened 4 years ago

himat commented 4 years ago

I have a strange situation where in my regular shell setup (bashrc/zshrc), I auto keep track of the last CDed directory so that whenever I open a new shell, it automatically opens to the same directly that I had last CDed. This is just a very convenient ability to me since it's pretty annoying in the default way where a new shell opens to ~/ and i have to figure out where to CD to.

But anyway, this is presenting a problem when using tmux-resurrect, but with resurrect, I want it to restore to the exact same paths that each of my tmux panes/windows were in. But due to my shell setup that I just described, whenever I restore my tmux sessions, ALL of the panes/windows get set to the same last directory that I was in.

Here's the simple shell code I have in my ~/.bashrc:

# Move to the last saved working directory if one exists
if [ -e ~/.last_cd ]
then
    cd "$(cat ~/.last_cd)"
fi

# Saves current directory between sessions
unalias cd 2>/dev/null # [not relevant] Prevent infinite loops if sourcing this in the same session
logged_cd() {
    cd "$@"
    pwd > ~/.last_cd
}

alias "cd"="logged_cd" # keep track of most recent directory

So is there a way for me to detect in my ~/.bashrc if this is a tmux-resurrect pane that is being started, and if so, I won't run the cd "$(cat ~/.last_cd)" command? Or alternatively, if I can run a command from tmux-resurrect after my shell script runs that will cd each pane into the tmux saved path for that pane, that could also work.

Thanks!