0
votes

I am using iTerm2 integrated with tmux. My normal working pattern is to first open up the iTerm2 terminal on my Mac, and then ssh to my dev Virtual Machine.

My dev VM has tmux installed, so that I can re-attach the tmux sessions to my dev VM.

The problem is when the first time I create the tmux session it will source ~/.bashrc with no problem. But if I clean detach tmux session, and later re-attach those tmux sessions, the ~/.bashrc will not be sourced.

I have included

if [ -f ~/.bashrc ]; then source ~/.bashrc; fi

in ~/.bash_profile, .profile, .bash_login.

And also included

set-option -g default-command "exec /bin/bash"

to ~/.tmux.conf

1
I figured out with the help of iTerm2 developer. Details can be found gitlab.com/gnachman/iterm2/issues/7367. Hope it helps other people having the same problem. - Psyduck

1 Answers

1
votes

As is implied by the verb "re-attach", your tmux session (and with it the Bash shell that runs in it) is kept running on your dev machine when you disconnect, so that you can later reconnect to the very same session. (That's the main feature of tmux: Normally, a shell is directly connected to your terminal or SSH session, so when you close / disconnect it, the shell has nothing to read from and output to, so it will have to exit. tmux provides a virtual terminal in between, so the shell has something to hang onto (even if nobody sees the output and nobody currently inputs anything), and tmux handles the session management.)

Applications (like Bash and also Vim) usually only read their configuration on startup. As Bash is kept running (you can verify that via ps -o etime --pid $$), it won't notice that you're reconnecting via tmux, and has no reason to reload its config - everything should still be defined and preserved within the tmux session. If you need to reload a (changed) configuration, you have to do this explicitly (source ~/.bashrc), or open up a new shell.