Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Prevent Tmux from Starting a Login Shell by Default

prevent-tmux-from-starting-a-login-shell-by-default.jpg

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.

Quick Jump:

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.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments