Tmux Tips and Tricks

Published:
Updated:
9 minute read

(If you are already familiar with tmux, and you are looking for a simple .tmux.conf, go to the end of this post.)

It’s been a long time since I wanted to start using tmux to see how it could be helpful, but Honestly, I haven’t felt the need to use it until just recently, when I had to run a long-running process on a remote server, and I didn’t want to keep my terminal open all the time. So, I decided to give tmux a try. And I’m glad I did! It’s a powerful tool that can help you manage multiple terminal sessions in a single window, and more. Here are some tips and tricks that I found useful.

For getting started, you can check tmux official documentation. Also, this one is a good cheat sheet. For the aweseome list of tmux resources, you can check this.

About tmux

tmux is a terminal multiplexer that allows you to run multiple terminal sessions in a single window. It’s similar to screen, but it has some additional features that make it more powerful and easier to use. With tmux, you can create multiple windows and panes, detach from a session and reattach later, and share sessions with other users.

tmux handles everything by a server-client model. The server runs in the background and manages all the sessions, windows, and panes. The client is the terminal that you interact with. You can have multiple clients connected to the same server.

To install tmux on your system:

# Ubuntu
sudo apt install tmux

Using tmux basics

I have provided some basic commands in this section, but in the following sections, I have provided the shortcuts and commands that you can use inside tmux. Also, I skipped writing about tmux commands in the status bar (similar to vim commands). If you want to see those, visit the cheat sheet I mentioned above. However, you can enter tmux command mode by pressing Ctrl+b :.

One useful command is : set mouse on to enable the mouse in tmux. You can enable it for all the sessions by adding -g to the command. (tmux set -g mouse on). To disable mouse, you can use : set mouse off.

Ok, back to the basics:

  • List all sessions with tmux ls
  • Start a new session with a name: tmux new -s mySession
  • Attach to a session:
tmux attach -t mySession
# or short
tmux a -t mySession
# Attach to the last session
tmux a
  • Detach from a session running tmux detach or pressing Ctrl+b d in the session.
  • Kill a session with tmux kill-session -t mySession.

Inside tmux

All commands in tmux start with Ctrl+b followed by another key (Unless the prefix key is changed in the configuration file). To see the full list of commands, you can press Ctrl+b followed by ?. The documents use the format C-b <command> to represent the Ctrl+b key combination. Also, M-<key> represents the Alt+<key> combination, and S-<key> shows the Shift+<key> combination.

Customizing tmux

One of the beauties of tmux is that you can customize it very easily. You can create a .tmux.conf file in your home directory and add your custom configurations, from changing the default key bindings to setting the colors of the status bar. Here is a simple .tmux.conf file:

# Set the prefix key to Ctrl+a -> people usually use this key binding
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Start windows and panes at 1, not 0
# Why you ask?! because 0 is pretty far away on the keyboard
set -g base-index 1
setw -g pane-base-index 1

# Enable mouse support
set -g mouse on

# Set the status bar colors
set -g status-bg black
set -g status-fg white

# Set the splitting commands, | for horizontal and - for vertical
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

# open new windows in the current path
bind c new-window -c "#{pane_current_path}"

# reload config file
bind r source-file ~/.tmux.conf

# Use Alt-arrow keys without prefix key to switch panes
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# set default terminal mode to 256 colors
set -g default-terminal "xterm-256color"
set -ga terminal-overrides ",xterm-256color:Tc"

# Visual bell, no sounds
set -g visual-bell on
set -g bell-action none

Managing tmux windows and sessions

After updating the .tmux.conf file, follow the table below:

CommandDescription
Ctrl+a wList all windows
Ctrl+a w 0-9Switch to window 0-9
Ctrl+a cCreate a new window
Ctrl+a &Close the current window
Ctrl+a nMove to the next window
Ctrl+a pMove to the previous window
Ctrl+a ,Rename the current window
Ctrl+a -Split the window vertically
Ctrl+a \|Split the window horizontally
Ctrl+a dDetach from the session
Ctrl+a sList all sessions
Ctrl+a $Rename the current session

Moving and resizing panes

After updating the .tmux.conf file to the content above, the commands from the following table can be used:

CommandDescription
Ctrl+a oMove to the next pane
Ctrl+a ;Move to the last active pane
Ctrl+a qShow pane numbers
Ctrl+a q <number>Activate the pane with the specified number
Ctrl+a xKill the current pane
Ctrl+a zZoom in/out the current pane
Ctrl+a !Move the current pane to a new window
Ctrl+a UpSwitch to the pane above the active pane
Alt+UpSwitch to the pane above the active pane (another way)
Ctrl+a DownSwitch to the pane below the active pane
Alt+DownSwitch to the pane below the active pane (another way)
Ctrl+a LeftSwitch to the pane on the left of the active pane
Alt+LeftSwitch to the pane on the left of the active pane (another way)
Ctrl+a RightSwitch to the pane on the right of the active pane
Alt+RightSwitch to the pane on the right of the active pane (another way)
Ctrl+a }Move the current pane right
Ctrl+a {Move the current pane left
Ctrl+a Ctrl+UpResize the current pane up
Ctrl+a Ctrl+DownResize the current pane down
Ctrl+a Ctrl+LeftResize the current pane left
Ctrl+a Ctrl+RightResize the current pane right

Automatic Startup

You can start tmux automatically when you open a terminal. To do this, add the following line to your .bashrc file:

# Start tmux if not already running
if [ -z "$TMUX" ]; then
    tmux attach || tmux new
fi

If you want to start tmux with a specific session, you can use the following command:

# Start tmux with a specific session
if [ -z "$TMUX" ]; then
    tmux attach -t mySession || tmux new -s mySession
fi

Fun Part

Now for the fun part, I have created some aliases to make it easier to manage tmux and its sessions. You can add these aliases to the .bashrc file:

# tmux aliases
alias ta='tmux attach -t'
alias tk='tmux kill-session -t'
alias tls='tmux ls'
alias tn='tmux new -s'
alias ttop='tmux attach -t top || tmux new -s top "top"'

# managing ssh sessions to remote servers
# First check if the session exists, if not create a new one and ssh to the server
# You can then later split the window and do whatever you want with the session, then detach and reattach later
alias tpprl='tmux attach -t pprl || tmux new -s pprl "ssh pprl"'
alias tmist='tmux attach -t mist || tmux new -s mist "ssh mist"'
alias tnarval='tmux attach -t narval || tmux new -s narval "ssh narval"'
alias tbeluga='tmux attach -t beluga || tmux new -s beluga "ssh beluga"'
alias tgraham='tmux attach -t graham || tmux new -s graham "ssh graham"'

# List all tmux sessions and windows at login
echo "Available tmux sessions:"
tmux ls

Another fun usecase: You may also combine the tmux session management with ssh control path to avoid getting 2FA prompts every time you connect to a server. You can check Ali’s post to understand the process.

References