Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:
How to Set Default Shell to Launch tmux Session on Startup
Last updated: August 16, 2024
1. Overview
tmux is a competent terminal multiplexer that allows us to control many terminal sessions from a single window. We can save time and increase efficiency by configuring our shell to automatically open a tmux session, especially if we frequently operate with lots of terminal windows. This configuration guarantees that our terminal sessions always begin in tmux, providing us access to all of its great features from the very beginning.
In this tutorial, we’ll look at how to configure the default shell to start a tmux session on startup.
2. Using Bash and Zsh
To automatically launch a tmux session with either Bash or Zsh shell on startup, we’ll need to modify the .bashrc and .zshrc configuration files respectively. Both files are executed whenever a new terminal session is launched.
Firstly, we’ll begin by opening the configuration file in our home directory with a text editor.
For Bash, we open .bashrc:
$ nano ~/.bashrc
For Zsh, we’ll open the .zshrc file:
$ nano ~/.zshrc
This would open the appropriate configuration file. At the end of this file, we’ll add the conditional statements:
if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then
tmux attach-session -t default || tmux new-session -s default
fi
In this example, we use the -v tmux &> /dev/null command to determine whether tmux is installed on our system. The &> /dev/null section guarantees that any output from this command is ignored, rendering the check quiet. It then checks if we’re already in a tmux session by evaluating [-z “$TMUX”].
The -z option returns true if the length of the $TMUX variable is 0, indicating that we’re not currently in a tmux session. If so, it attempts to attach to an existing tmux session called default with the tmux attach-session -t default command. Lastly, if no such session exists, the command tmux new-session -s default creates a new session named default.
Additionally, by adding this script to our configuration file, every time we open a new terminal window, it will first check to see if we’re currently in a tmux session. If not, it will either join an existing default session or start a new one. This configuration ensures that we’re constantly working within a tmux session, which improves our workflow and productivity by making all of tmux’s capabilities readily available.
3. Using Fish
We can set up the Fish shell to automatically start a tmux session on startup. To achieve this, we need to modify the config.fish file, which contains user-specific settings and executes whenever we start a new Fish session.
We’ll start by opening the config.fish file in our home directory with a text editor:
$ nano ~/.config/fish/config.fish
This command would open up the config.fish file. At the end of this file, we can then write conditional statements:
if type -q tmux
if not test -n "$TMUX"
tmux attach-session -t default; or tmux new-session -s default
end
end
In this example, we start with the type -q tmux statement to check whether tmux is installed on our system. The -q parameter makes the check quiet by silencing all output. Next, it checks if we’re already in a tmux session by evaluating if not test -n “$TMUX”.
The test -n $TMUX checks if $TMUX is not empty, indicating we’re in a tmux session. If so, it will try to attach to the default session using tmux attach-session -t default. Additionally, if no such session exists, the tmux new-session -s default command creates a new session named default.
Furthermore, configuring the Fish shell to open a tmux session on startup makes it easier to manage many terminal windows which increases general efficiency.
4. Conclusion
Understanding how to set the default shell to start a tmux session on startup improves efficiency and simplifies our work process.
In this article, we’ve examined several methods for configuring tmux to launch automatically with Bash, Fish, and Zsh, ensuring that we always start our terminal sessions with tmux’s great features at our disposal.
Furthermore, Implementing these approaches will save time, improve session management, and create a more organized development environment.