Tmux — Installation and Customization + Nord theme
Greetings!

In the previous note 📝 we talked about terminal multiplexers and what tasks they perform. In today’s article I’ll demonstrate the installation and customization of the most popular tool in this category — Tmux 🪟. As usual, everything is already suffered through configured, and to replicate it yourself, you’ll just need to run a few commands🧑💻.

The demonstration given in this article was performed in the Linux Mint 22 distribution environment with Tmux version 3.4 ✍️.

Preface

I really love the ❄️ theme from the developers at Arcticicestudio⛄️ and prefer to style my system and apps with this palette😌. Tmux is no exception, so for correct and harmonious display of the configuration like mine, you’ll need:

For those who are just getting acquainted with Tmux, let me briefly list its entities🧐:

We can proceed with the installation.

Installing Tmux

Tmux is almost always in the standard repositories, and in many Linux distributions it’s even pre-installed👌.

Open a terminal and run:

BASH
sudo apt update && sudo apt install -y curl git xclip tmux
Click to expand and view more

Downloading the Config and Launching

Now let’s use the curl utility to download the Tmux config file from my GitHub 😇 repository:

BASH
curl --create-dirs -fLo \
    ~/.config/tmux/tmux.conf \
    https://raw.githubusercontent.com/r4ven-me/dots/main/.config/tmux/tmux.conf
Click to expand and view more

All that’s left is to launch Tmux😳. The first launch will take some time⏳, since the plugin manager and then the plugins themselves will be downloaded via this manager.

The config is set up so that when new plugins are added, they’ll be installed automatically😌 during Tmux startup/restart. To install plugins manually, use prefix+I.

To create a new named session, use the command:

BASH
tmux new -s Work
Click to expand and view more

Where:

You should get something like this:

That’s it😃, the setup is done! Now you can actively use it:

And if you have a wide monitor, it’s just amazing 🔥🔥🔥!

You could live in a terminal like this😎

To exit tmux without closing the sessions, send the detach command with a special hotkey: first press the so-called prefix key combination, Ctrl+b by default, and then immediately press the d key, shortened as: prefix+d.

Yes, that’s the hotkey style of Tmux🤷‍♂️. First you press the prefix key, then the action/command key. You need to get used to this approach. The prefix key can be changed in the config. But it’s not recommended, in order to preserve universality🫠.

To return to a running session, use the command:

BASH
tmux attach -t Work || tmux new -s Work
Click to expand and view more

Where:

I recommend creating a convenient alias for your shell right away:

BASH
echo 'alias T="tmux attach -t Work || tmux new -s Work"' >> ~/.profile

source ~/.profile
Click to expand and view more

Replace ~/.profile with the file containing your shell environment setup parameters if needed☝️.

Now you can connect to the existing session named Work simply with the single-letter command T.

Description of the Configuration and Overview of the tmux.conf File

What This Config Adds/Changes

General settings📖:

Mouse control🐁:

Keyboard shortcuts🎹:

Some key combinations have been slightly extended. See more details below: Item 6. Custom Tmux Hotkeys.

Plugin list📋:

Additionally📦:

Contents of the tmux.conf File

For better understanding, all parameters are commented in Russian😌:

BASH
#########################
### TMUX CLI COMMANDS ###
#########################

# Reload the tmux config                      tmux source ~/.config/tmux/tmux.conf
# Attach to an existing session               tmux attach -t session_name
# Kill all tmux processes                     pkill -f tmux
# Install specified plugins via TPM           prefix + I
# Run an arbitrary command on startup         run-shell "~/your/command.sh"
# is set in tmux.conf

##########################
### MAIN SETTINGS ###
##########################

set-option -g default-terminal "screen-256color" # Enable 256 colors in the terminal
#set -g default-terminal 'tmux-256color'
#set -g default-terminal 'xterm-256color'
set -ga terminal-overrides ',*:Tc'   # Enable TrueColors
set -ga terminal-overrides '*:Ss=\E[%p1%d q:Se=\E[ q' # Support for changing cursor shape
set -g base-index 1                  # Start window numbering from 1
set -g pane-base-index 1             # Start pane numbering from 1
set -g aggressive-resize on          # Automatically resize panes when the window is resized
set -g history-limit 10000           # Keep 10,000 lines in the scrollback buffer
set -g set-titles on                 # Enable automatic title updates
set -g mouse on                      # Enable mouse support
set -g mode-style "fg=black,bg=blue" # Set text selection colors in copy mode
set -g status-position top           # Place the status bar at the top
set -g default-shell /usr/bin/zsh    # Set the default shell
set -g default-command /usr/bin/zsh  # Set the default shell command
set -g mode-keys vi                  # Use vi key bindings in copy mode
set -g status-keys vi                # Use vi key bindings for tmux prompts
set -g @shell_mode 'vi'              # Explicitly set the shell mode to vi
set-environment -g LC_CTYPE "ru_RU.UTF-8" # Set the environment encoding for Russian
#set -g pane-active-border-style fg=blue # Set the active border color: example with bg 'fg=blue,bg=green'
#set -s set-clipboard off             # Clipboard mode on|off|external
#set -g display-time 100              # Set the delay after pane focus change (default 750 ms)

## Additional status bar
#setw -g pane-border-status bottom    # Border placement
#setw -g pane-border-format '─'       # Border style

########################
### MOUSE CONTROL ###
########################

# Open a new window with a double-click on the status line
bind-key -n DoubleClick1Status new-window

# X11-specific bindings
%if "#{==:#{XDG_SESSION_TYPE},x11}"
    # Middle click pastes from the clipboard
    unbind-key MouseDown2Pane
    bind-key -n MouseDown2Pane \
        run 'tmux set-buffer "$(xclip -out -sel clipboard)"; tmux paste-buffer'

    # Middle click cancels copy mode and pastes from the clipboard
    bind-key -T copy-mode-vi MouseDown2Pane \
        select-pane \; \
        send -X cancel \; \
        run 'tmux set-buffer "$(xclip -out -sel clipboard)"; tmux paste-buffer'

    # Copy selection to the clipboard without clearing it
    bind -T copy-mode-vi MouseDragEnd1Pane \
        select-pane \; \
        send-keys -X copy-pipe-no-clear "xclip -in -sel clipboard"

    # Clear selection on left-click
    bind -T copy-mode-vi MouseDown1Pane \
        select-pane \; \
        send-keys -X clear-selection
   
    # Double and triple left-click to select a word/line in copy mode
    bind-key -T copy-mode-vi DoubleClick1Pane \
        select-pane \; \
        send-keys -X select-word \; \
        send-keys -X copy-pipe-no-clear "xclip -in -sel clipboard"

    bind-key -T copy-mode-vi TripleClick1Pane \
        select-pane \; \
        send-keys -X select-line \; \
        send-keys -X copy-pipe-no-clear "xclip -in -sel clipboard"
%else # Bindings for sessions not using X11
    # Middle click pastes from the buffer
    unbind-key MouseDown2Pane
    bind-key -n MouseDown2Pane paste-buffer

    # Middle click cancels copy mode and pastes from the buffer
    bind-key -T copy-mode-vi MouseDown2Pane \
        select-pane \; \
        send -X cancel \; \
        paste-buffer

    # Copy selection without clearing it
    bind -T copy-mode-vi MouseDragEnd1Pane \
        select-pane \; \
        send-keys -X copy-selection-no-clear

    # Clear selection on left-click
    bind -T copy-mode-vi MouseDown1Pane \
        select-pane \; \
        send-keys -X clear-selection

    # Double and triple left-click to select a word/line in copy mode
    bind-key -T copy-mode-vi DoubleClick1Pane \
        select-pane \; \
        send-keys -X select-word \; \
        send-keys -X copy-selection-no-clear

    bind-key -T copy-mode-vi TripleClick1Pane \
        select-pane \; \
        send-keys -X select-line \; \
        send-keys -X copy-selection-no-clear
%endif

#########################
### KEY COMBINATIONS ###
#########################

bind-key 'v' copy-mode # 'prefix+v' to enter copy mode
bind-key -T copy-mode-vi 'v' send -X begin-selection # 'v' to start selection in copy mode

# X11-specific bindings
%if "#{==:#{XDG_SESSION_TYPE},x11}"
    # 'Enter' and 'y' in copy mode, copies the selection to the clipboard via xclip and cancels copy mode
    bind-key -T copy-mode-vi 'Enter' \
        select-pane \; \
        send -X copy-pipe-and-cancel "xclip -in -sel clipboard"

    bind-key -T copy-mode-vi 'y' \
        select-pane \; \
        send -X copy-pipe-and-cancel "xclip -in -sel clipboard"

    # ']' and 'P' (capital) in normal mode, pastes from the clipboard via xclip
    bind-key ']' \
        run 'tmux set-buffer "$(xclip -out -sel clipboard)"; tmux paste-buffer'

    bind-key 'P' \
        run 'tmux set-buffer "$(xclip -out -sel clipboard)"; tmux paste-buffer'

    # ']' and 'p' in copy mode, cancels copy mode and pastes from the clipboard via xclip
    bind-key -T copy-mode-vi ']' \
        select-pane \; \
        send -X cancel \; \
        run 'tmux set-buffer "$(xclip -out -sel clipboard)"; tmux paste-buffer'
    
    bind-key -T copy-mode-vi 'p' \
        select-pane \; \
        send -X cancel \; \
        run 'tmux set-buffer "$(xclip -out -sel clipboard)"; tmux paste-buffer'
%else # If not on X11
    # 'y' in copy mode, copies the selection to the tmux buffer and cancels copy mode
    bind-key -T copy-mode-vi 'y' \
        select-pane \; \
        send -X copy-selection-and-cancel

    # Pressing 'P' (capital) in normal mode pastes from the tmux buffer
    bind-key 'P' paste-buffer

    # Pressing 'p' in copy mode cancels copy mode and pastes from the tmux buffer
    bind-key -T copy-mode-vi 'p' \
        select-pane \; \
        send -X cancel \; \
        paste-buffer
%endif

# Vi-style pane navigation
bind-key -r -T prefix h select-pane -L
bind-key -r -T prefix j select-pane -D
bind-key -r -T prefix k select-pane -U
bind-key -r -T prefix l select-pane -R

# Resizing panes with vi-style keys
bind-key -r -T prefix H resize-pane -L 2
bind-key -r -T prefix J resize-pane -D 2
bind-key -r -T prefix K resize-pane -U 2
bind-key -r -T prefix L resize-pane -R 2

# Manual bindings for saving/restoring tmux-resurrect
# 'prefix+F5' to save
bind-key -T prefix F5 run-shell \
    '~/.config/tmux/plugins/tmux-resurrect/scripts/save.sh && mv -f $(find ~/.local/share/tmux/resurrect -type f -name "tmux_resurrect_*.txt" -size +0c | sort | tail -1) ~/.local/share/tmux/resurrect/main.txt && tmux display-message "Environment saved manually!"'

# 'prefix+F6' to load
bind-key -T prefix F6 run-shell \
    'ln -sf ~/.local/share/tmux/resurrect/main.txt ~/.local/share/tmux/resurrect/last && ~/.config/tmux/plugins/tmux-resurrect/scripts/restore.sh'

#######################
### PLUGIN LIST ###
#######################

# Plugins
set -g @plugin 'tmux-plugins/tpm'            # Plugin manager
set -g @plugin 'tmux-plugins/tmux-sensible'  # Sensible defaults for tmux
set -g @plugin 'arcticicestudio/nord-tmux'   # Nord theme for tmux
set -g @plugin 'tmux-plugins/tmux-resurrect' # Save and restore the tmux environment
set -g @plugin 'tmux-plugins/tmux-continuum' # Automates tmux-resurrect save and restore actions

##########################
### PLUGIN SETTINGS ###
##########################

# tmux-resurrect settings
# Clean up old session files
run-shell \
    'find ~/.local/share/tmux/resurrect/ -type f -name "*.txt" ! -name "main.txt" -mtime +3 -delete'
#set -g @resurrect-capture-pane-contents 'on' # Restore the contents of previous sessions
set -g @resurrect-strategy-nvim 'session' # Use sessions to restore Neovim
set -g @resurrect-processes 'ssh ranger mc tmux cmatrix ipython' # Processes to restore

# tmux-continuum settings
set -g @continuum-boot 'on'          # Auto-start tmux via systemd
set -g @continuum-restore 'on'       # Auto-restore tmux sessions
set -g @continuum-save-interval '60' # Auto-save every 60 minutes

# Make sure the TPM plugin is installed
if "test ! -d ~/.config/tmux/plugins/tpm" \
    "run 'git clone https://github.com/tmux-plugins/tpm ~/.config/tmux/plugins/tpm'"

# Auto-install other plugins on a new computer
run '~/.config/tmux/plugins/tpm/tpm'
run '~/.config/tmux/plugins/tpm/bin/install_plugins'

# Initialize TPM (keep this line at the very bottom of tmux.conf)
run '~/.config/tmux/plugins/tpm/tpm'
Click to expand and view more

Reminder: the latest version of tmux.conf is also available on my GitHub.

Basic Tmux Hotkeys

Below are the frequently used basic tmux commands/key combinations. If you plan to work regularly in a tmux environment, I strongly recommend memorizing them🤯.

Prefix❗️:

Ctrl-b — prefix;

Window management 🪟:

Pane management 🎛:

Session management 📚:

Copy and paste 📋:

Additionally 📦:

Custom Tmux Hotkeys

Added/changed commands/keys from my config.

Copy mode and clipboard interaction 📝:

Navigation between panes (vi-style) 🎛:

Resizing panes (vi-style)🎛, the command key can be pressed repeatedly:

Saving and restoring the environment via tmux-resurrect 💾:

Afterword

Phew😮‍💨. Today our Linux administrator’s arsenal has gained another useful tool🧑💻. It’s extremely convenient for maintaining systems📺 that have no GUI, for example on servers running 24/7. You launch all the necessary utilities once, and afterward you just connect to the running session with a short alias😌.

Thanks for reading😇. I wish you success in mastering new tools that simplify routine tasks and boost efficiency💪.

Useful Materials

Copyright Notice

Author: Ivan Cherniy

Link: https://r4ven.me/en/dots/tmux-ustanovka-i-kastomizaciya-nord-theme/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut