Site Logo
mnjm

TMUX - My configurations

Posted on 3 mins

Tmux

What is TMUX?

TMUX is a short form for Terminal MUtipleXer. It allows you to switch simply divide up your terminal real estate into tiny pseudo-terminals running it own independent shells.

TMUX Screenshot

Features:

  1. Terminal Sessions - When you open/run tmux, a tmux session gets created. Each pane you create in the tmux screen gets added to this session. You can leave this session and return to it later, with its state preserved and all the shells (programs) running in that tmux continuing in the background. This is useful for switching seamlessly between projects, as I have one session per project.

  2. Using TMUX on remote machines - Since TMUX is a shell program, you can use it when SSH’d into a remote machine, allowing you to “multiplex” (open a bunch of shells) from a single SSH connection. If your SSH connection drops, you can SSH back and simply attach back to the session you were using before without worrying about your remote apps being stopped.

  3. Terminal app independent - As TMUX is a terminal multiplexer that runs as a shell app, you can use it in any terminal emulator and utilize its keybinds. This is especially useful if you use multiple terminal emulators for work and personal use.

  4. Session sharing - Sharing TMUX sessions to collaborate with a colleague in real time - You can share your tmux session with a colleague like sharing the terminal emulator screen. All they have to do is SSH into your machine and connect to your session.

  5. Configurable - You can also preconfigure sessions, windows, panes, and apps on the pane to your liking without needing to set it up every time.

Install and Using TMUX

On Ubuntu/Debian distros, run sudo apt install tmux and confirm if prompted.

To run/start a tmux session, run tmux.

Check this link as a guide for using tmux.

My changes and additions to TMUX config

Cheatsheet for Tmux

While default keybinds work for most, I have configured them to work in tandem with my Vim configuration, making it easier to memorize. Additionally, I’ve themed them according to my liking.

TMUX Screenshot

You can find my tmux configuration file here .

Some keybind changes:

You can sideload config file using tmux -f <config file> ....

Custom Session Create / Select using FZF

I find the built-in session selector rather limiting in terms of functionality. Therefore, I’ve wrote my own session selector based on Fzf. It enables easy switching between TMUX sessions within a few keystrokes and can also spin up a new session if one is not found.

#!/usr/bin/env zsh

sessions=$(tmux list-session -F "#{session_name}" 2>/dev/null)
selected=$(echo $sessions | fzf --height 100% --reverse --no-multi --print-query \
    --preview "tmux capture-pane -pt {}" --preview-window right,90% | tail -n1)
if [[ -n $selected ]]; then
    if [ -z "$TMUX" ]; then
        tmux new -As $selected
    else
        if  ! tmux has-session -t $selected 2>/dev/null; then
            tmux new -ds $selected
        fi
        tmux switch-client -t $selected
    fi
fi
unset sessions selected

To use it, save it somewhere accessible to tmux with exec permissions and bind it to a key (with prefix) using

bind <key> display-popup -h 90% -w 90% -E '<script location>'

Resources