What is tmux? Link to heading

tmux, or Terminal Multiplexer, is the most popular representative of a whole family of tools used to emulate multiple terminals inside a single session. You can find its code on GitHub. If you use some Linux distribution or macOS, tmux is either already installed or available through your favorite package manager.

In this article I want to show how I use tmux and how I configure it. If you want to dig deeper into the topic, the best place is, of course, the project homepage and official documentation. You will find everything you need there, including a simple configuration generator that can help you get started faster.

Use cases Link to heading

As I mentioned above, tmux is used to manage multiple terminals inside a single session. It gives you a lot of flexibility if you do not want to constantly click through tabs in iTerm or another terminal emulator with similar functionality. It also works on remote systems when you log in over SSH. From my point of view, it helps me categorize many sessions and windows more clearly and makes switching between them faster.

Let’s get started Link to heading

First impressions Link to heading

To start tmux, simply run the tmux command in your terminal. Once you do that, you will notice that almost nothing has changed. That “almost” matters here. There is now a status bar at the bottom of the terminal screen. It will show all information about active windows in the running session. It is not particularly pretty at first, but it is worth getting used to the default look, especially if you often log in to machines, for example servers, that you do not manage, or if you do not feel like copying your configuration file everywhere. Later I will also do a little bit of polishing around how it looks, but for now let’s move on to the basics.

Basic usage Link to heading

The most important thing to remember is the so-called prefix, which by default is the Ctrl + b key combination. For many people, the first thing they do in tmux is change the prefix to Ctrl + a, usually because they find it more ergonomic. Others prefer to build muscle memory around the defaults, so they do not get lost later without their own configuration. In my opinion, both options are fine as long as they work for you. I change the prefix because I also find the default one a bit unnatural.

Alright, so what is this prefix actually for? Whatever it is, it tells tmux that you now want to send commands to tmux itself, not to the active terminal. The most basic actions are:

  • Ctrl + b % - splits the active pane into left and right parts
  • Ctrl + b " - splits the active pane into top and bottom parts
  • Ctrl + b <arrow> - moves between panes in the active window
  • Ctrl + b c - creates a new window
  • Ctrl + b <number> - switches the active window
  • Ctrl + b : - opens the command line, where you can run actions without predefined keyboard shortcuts

Another thing many people change is some of the shortcuts above. Personally, I prefer changing splits to | for a left-right split and - for a top-bottom split. It simply feels more visual to me and is easier to remember, but just like with the prefix, the problem appears when you do not have your configuration file somewhere.

How I use tmux Link to heading

My way of working with this tool is pretty simple and comes down to a few rules:

  • one project inside one session
  • the first window is always the editor, in my case neovim, though I will write about that in another article, plus a terminal pane for quickly running commands
  • the next windows are for SSH or long-running commands, such as serving an application

And that is really it, but it has made working with many windows much faster for me.

Configuration and making it nicer Link to heading

Now let’s take care of how tmux looks and behaves. I am going to show my current configuration here. It is quite minimal, but enough for me. I will go through it and explain what happens in each part. This is also a good moment to mention a nice article, Ham Vocke | Make tmux Pretty and Usable, which I read around the time I was building my own configuration.

# Simple tmux configuration
# based on: https://hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/
#
# ---
# Explanation:
# This configuration replaces default prefix 'b' with 'a',
# changes indexing start from 0 to 1 (easier to use with keyboard),
# rebinds splits to more descriptive keys '-' and '|',
# enables mouse interactions,
# adds custom subtle theming
# ---
# Instructions:
#
# to use tmux at bash start add below lines from comment to .bashrc file (or other rc of your shell)
# if [ -x "$(command -v tmux)" ] && [ -n "${DISPLAY}" ] && [ -z "${TMUX}" ]; then
#     exec tmux new-session -A -s "${USER}" >/dev/null 2>&1
# fi
#
# You can also add this nifty little alias to your shell rc file:
# alias tmx='tmux attach-session -t default || tmux new -s default'
# this will attach to 'default' session or create one if it doesn't exist


unbind C-b

# Start windows and panes at 1, not 0 for easier keyboard navigation
set -g base-index 1
setw -g pane-base-index 1

# set a as prefix key
set-option -g prefix C-a
bind-key C-a send-prefix

# set natural splitters | and -
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

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

# fast pane switch without prefix
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

# mouse control
set -g mouse on

# add style
set-option -g status-position top
set -g status-left-length 40
set -g status-style bg=default,fg=default
set -g status-right "#[fg=#81a1c1, bg=default, bold] 󰃰 %Y-%m-%d 󱑀 %H:%M "
set -g status-left "#[fg=#a3be8c, bold] #S #[fg=#b48ead]| #[fg=#88c0d0]#(whoami)  #(hostname) #[fg=#b48ead]|"
set -g window-status-current-format "#[bg=#a3be8c,fg=black]  #I:#W #[bg=default,fg=default]"
set -g window-status-format " #[fg=#88c0d0] #I:#W "

The comments mostly describe what is going on there, and they also add a small bonus in the form of code that starts tmux whenever you open a terminal. But one thing at a time.

unbind C-b

# Start windows and panes at 1, not 0 for easier keyboard navigation
set -g base-index 1
setw -g pane-base-index 1

# set a as prefix key
set-option -g prefix C-a
bind-key C-a send-prefix

In this part, you remove Ctrl + b as the prefix, set window indexing to start from 1 instead of the default 0, and set the new prefix to Ctrl + a.

# set natural splitters | and -
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

Then you set window splitting to more natural shortcuts and remove the old ones.

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

You add a shortcut for quickly reloading the configuration. This is useful when you are working on changes and testing different options. To reload the new configuration, you press Ctrl + a r. From this point on, you use the new prefix, Ctrl + a, not the default Ctrl + b.

# fast pane switch without prefix
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

# mouse control
set -g mouse on

You also set up better navigation between panes. Now Alt + <arrow>, or Option instead of Alt on a Mac, lets you quickly switch between panes inside the active window. The last line adds mouse support. In general, I try to avoid using it, but sometimes it is useful for selecting some text, logs, or whatever else.

# add style
set-option -g status-position top
set -g status-left-length 40
set -g status-style bg=default,fg=default
set -g status-right "#[fg=#81a1c1, bg=default, bold] 󰃰 %Y-%m-%d 󱑀 %H:%M "
set -g status-left "#[fg=#a3be8c, bold] #S #[fg=#b48ead]| #[fg=#88c0d0]#(whoami)  #(hostname) #[fg=#b48ead]|"
set -g window-status-current-format "#[bg=#a3be8c,fg=black]  #I:#W #[bg=default,fg=default]"
set -g window-status-format " #[fg=#88c0d0] #I:#W "

At the very end, there is a visual improvement. I used to use a plugin with the Nord theme, but I decided it made things even more complicated, so I switched to a simpler solution.

What you see here is moving the status bar to the top of the window, a light color adjustment, and formatting information such as the active session, user, host, and windows in that session on the left, with the date and clock on the right. It is also sprinkled with a few Nerd Font icons, so you need a font with icons for them to render correctly.

That is enough to start Link to heading

This configuration does not turn tmux into a Swiss Army knife, but it gives me exactly what I need day to day: fast context switching, sensible window splitting, and a slightly more readable status bar. It is best to treat it as a starting point, play around with the shortcuts, and keep only the things that actually speed up your work.