Prevent Tmux from Starting a Login Shell by Default
We can instruct Tmux to start your configured shell in a way that doesn't execute your profile every time you launch a pane or window.
Prefer video? Here it is on YouTube.
By default every time you launch a pane / window, Tmux launches a login shell
which means any code you have defined in your shell’s profile (.bash_profile
,
.zprofile
, etc.) runs. The above is true at least with Tmux 3.5a (2024) and
someone reported
this
way back in 2014.
That can slow down starting a session depending on what you’re doing but more importantly if you’re loading a window manager in your profile so it starts after logging in, you may end up spawning multiple copies of it unless you write defensive conditions to protect against that (not a bad idea in any case).
I noticed this recently when I installed native Linux on a laptop. Within WSL 2 I didn’t think twice about this because there weren’t any real side effect inducing code running in my profile. All I really do is define a few environment variables.
# Tmux Config
You can add this to your Tmux config. It will ensure Tmux starts your configured shell in a way that doesn’t invoke your login profile. Your usual shell’s rc file will still run on every pane / window and that is expected.
# We don't need to add `-i` to make it interactive because Tmux starts a PTY (pseudo-terminal)
# out of the box which is interactive.
set -g default-command "${SHELL}"
I’ve added this to my dotfiles.
You can confirm this by adding something like echo "HELLLLLLLLLLLLO"
to your
shell’s profile file, don’t set the above config item and notice every time you
launch a pane or window it will get printed. Then you can apply the above
config, reload your Tmux config or kill / start Tmux and notice how it doesn’t
get printed.
You can also drop an echo into your shell’s rc file to confirm it still runs
with and without the new default-command
config option.
The video below demos the above.
# Demo Video
Timestamps
- 0:45 – Setting Tmux’s default command to your shell
- 1:24 – Demonstrating the before / after
What’s your default command set to in Tmux? Let me know below.