Tmux

Terminal multiplexer

Linux
Author

Shane Oh

Published

June 5, 2023

Start off with installing tmux with homebrew on Mac.

brew install tmux
session
window
pane
pane
pane

Intro

There are three main concepts in tmux: session, window, and pane. Start by entering tmux in the terminal.

The screen you see right after entering the command is a pane in a window.

Pane

Split the pane using Ctrl-b % and Ctrl-b ".

The Ctrl-b works as a prefix to send a command — later below.

Navigate through the panes using Ctrl-b ↑, Ctrl-b →, Ctrl-b ↓, Ctrl-b ←.

Window

Open a new window with Ctrl-b c.

See the windows you opened on the bottom green bar? This gives us some information about the windows in the session. The current window you are seeing is marked with an *.

Navigate through the windows using Ctrl-b n — n is for next. This will cycle through all the windows in the current session. Reverse navigate with Ctrl-b p — p is for previous. You can also navigate directly to a window using the index. The bindings will be like Ctrl-b 1.

Session

From the very first, right after the tmux command, you are attached to a session with an auto generated index. Detach the session with the Ctrl-b d command — and d is for detach. It’s almost the same as starting the bash session and detaching with the exit command. Tmux can also be detached using the exit command, but this can be tedious because each split pane needs to be closed individually with the exit command.

Configurations

Feeling comfortable with the keybindings? I hope not. The default keybindings can put significant stress on your left pinky. This brings us to the .tmux.conf file for some configuration.

~/.tmux.conf
unbind-key C-b
set -g prefix C-a
1bind-key C-a send-prefix

2set -g mouse on
3set -g base-index 1
set -g renumber-windows on
set -g default-terminal "tmux-256color"

4bind r source-file ~/.tmux.conf \; display-message ".tmux.conf reloaded!"

5bind '\' split-window -h -c "#{pane_current_path}"
6bind - split-window -v -c "#{pane_current_path}"

7bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

8bind x kill-pane
9bind X kill-window
1
Replace the prefix key from Ctrl-b to Ctrl-a. When you are using a keyboard like HHKB, this will come in pretty handy. Let’s talk more about HHKB in some other posts.
2
Pretty straight forward. Helps you navigate split panes with a mouse.
3
The default is 0. For me, 0 key is a bit far for everyday use.
4
Reload the .tmux.conf file and display a message when done — similar to something like source .bashrc.
5
Split the window horizontally using the current pane’s path.
6
Split the window vertically using the current pane’s path.
7
Move between panes using Vim-style keybindings.
8
Kill the current pane.
9
Kill the current window.

Personal preference on the following one.

.zshrc
tmux() {
    if [ "$#" -eq 0 ]; then
1        command tmux new-session -A -s default
    else
        command tmux "$@"
    fi
}
1
Attach a session named default when tmux is typed.