Terminal: Tmux
A way to standardize the window and session management inside the terminal.
I’m recently new to using Tmux in my terminal sessions, since the main argument used to be that you can reconnect to all of the servers you do work on from different machines. That was a use case that I didn’t feel resonated with me since I spend most of my time on my own machine.
But, when digging into Neovim I came across a few Tmux tutorials too and started to see another point. With the help of a little program like Tmuxifier I can set up all of my different setups needed at work once and then attach to these when needed, that makes the start up time close to zero when switching contexts and have made my life easier.
I also like that I can jump around between terminal emulators like iTerm2, Ghostty, and Wezterm and have the same setup.
After a brew install tmux
we can:
tmux
- starttmux ls
- show sessionstmux attach
- attach to old sessiontmux attach -t name
- attach toname
session
The default prefix in tmux is Ctrl
+ B
. To see all commands, hit the prefix and :
and use the command list-keys
. Another option is to list all commands with the list-commands
command.
Working with Sessions, Windows, and Panes inside tmux
Here is an overview of how you can think of sessions, windows, and panes in Tmux.
┌────────────────────────────────────────────────────┐ ┌────────────────────────────────────────────────────┐ ┌────────────────────────────────────────────────────┐
│ Session #1 │ │ Session #2 │ │ Session #3 │
│ ┌────────────────────────────────────────────────┐ │ │ ┌────────────────────────────────────────────────┐ │ │ ┌────────────────────────────────────────────────┐ │
│ │ Window #1 │ │ │ │ Window #1 │ │ │ │ Window #1 │ │
│ │ ┌─────────────────────┐┌─────────────────────┐ │ │ │ │ │ │ │ │ │ │
│ │ │ ││ │ │ │ │ │ │ │ │ │ │ │
│ │ │ ││ │ │ │ │ │ │ │ │ │ │ │
│ │ │ Pane #1 ││ Pane #2 │ │ │ │ │ │ │ │ │ │ │
│ │ │ ││ │ │ │ │ │ │ │ │ │ │ │
│ │ │ ││ │ │ │ │ │ │ │ │ │ │ │
│ │ └─────────────────────┘└─────────────────────┘ │ │ │ │ │ │ │ │ │ │
│ └────────────────────────────────────────────────┘ │ │ └────────────────────────────────────────────────┘ │ │ └────────────────────────────────────────────────┘ │
│ ┌────────────────────────────────────────────────┐ │ └────────────────────────────────────────────────────┘ │ ┌────────────────────────────────────────────────┐ │
│ │ Window #2 │ │ │ │ Window #2 │ │
│ │ │ │ │ │ ┌────────────────────────────────────────────┐ │ │
│ │ │ │ │ │ │ Pane #1 │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ └────────────────────────────────────────────┘ │ │
│ │ │ │ │ │ ┌────────────────────────────────────────────┐ │ │
│ │ │ │ │ │ │ Pane #2 │ │ │
│ │ │ │ │ │ └────────────────────────────────────────────┘ │ │
│ └────────────────────────────────────────────────┘ │ │ └────────────────────────────────────────────────┘ │
└────────────────────────────────────────────────────┘ └────────────────────────────────────────────────────┘
Sessions
- List sessions:
<Prefix> s
- Rename session:
<Prefix> $
Windows
- List windows:
<Prefix> w
- Create window:
<Prefix> c
- Next window:
<Prefix> n
- Previous window:
<Prefix> p
- Rename current window:
<Prefix> ,
- Select window by number
<Prefix> 0-9
Panes
- Navigate panes:
<Option> <Left>/<Right>/<Up>/<Down>
- Split horizontal:
<Prefix> -
- Split vertical:
<Prefix> |
Config file
Located in ~/.tmux.conf
# ---- Options ---------------------------------------------------------------
# Set color mode
set-option -g default-terminal "tmux-256color"
set-option -ag terminal-overrides ",xterm-256color:RGB"
set-option -g allow-passthrough on
set-option -ga update-environment TERM
set-option -ga update-environment TERM_PROGRAM
# Add mouse support (and do not exit copy mode when dragging with the mouse)
set-option -g mouse on
unbind -T copy-mode-vi MouseDragEnd1Pane
# Display the status bar at the top of the screen
set-option -g status-position top
# Remove delay for exiting insert mode with ESC in Neovim
set-option -sg escape-time 0
# Start windows and panes at 1, not 0
set-option -g base-index 1
set-option -g pane-base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on
# Set vi mode
set-window-option -g mode-keys vi
# ---- Key maps --------------------------------------------------------------
# Reload config
unbind r
bind r source-file ~/.tmux.conf
# Set prefix to Ctrl-Space
unbind C-b
set-option -g prefix C-Space
bind C-Space send-prefix
# Kill sessions with Ctrl-q
bind -n C-q kill-session
# Split windows open pane in same path as current window
unbind %
bind | split-window -h -c "#{pane_current_path}"
unbind '"'
bind - split-window -v -c "#{pane_current_path}"
# Resize panes
bind j resize-pane -D 5
bind k resize-pane -U 5
bind l resize-pane -R 10
bind h resize-pane -L 10
# Use prefix + m to maximize pane
bind -r m resize-pane -Z
# 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
# Shift arrow to switch windows
bind -n S-Left previous-window
bind -n S-Right next-window
# Keybindings for vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel
bind-key -T copy-mode-vi 'v' send -X begin-selection # start selecting text with "v"
bind-key -T copy-mode-vi 'y' send -X copy-selection # copy text with "y"
# ---- Styling ---------------------------------------------------------------
# Basic colors of the Statusbar
set-option -g status-style bg=default,fg=black
# Show the window list centered between the left and the right section
set-option -g status-justify absolute-centre
# Style and set contents on the left section
set-option -g status-left "\
#[bg=default,fg=darkgrey]// \
#[bg=default,fg=brightblue]#S \
#[bg=default,fg=darkgrey]// \
"
# Style and set contents on the right section
set-option -g status-right "\
#[bg=default,fg=darkgrey] // \
#[bg=default,fg=brightblue]tmux\
#[bg=default,fg=darkgrey] // \
"
#
# Set max length of left and right section
set-option -g status-left-length 100
set-option -g status-right-length 100
# Style and set content for the inactive windows
set-option -g window-status-format " \
#I\
#[fg=black]:\
#[fg=darkgrey]#W \
"
# Style and set content for the active windows
set-option -g window-status-current-format "\
#[bg=default, fg=brightblue]░ #I\
#[fg=brightblue]:\
#[fg=brightblue]#W \
#[fg=default] \
"
# Panes
set-option -g pane-border-style 'fg=black'
set-option -g pane-active-border-style 'fg=brightyellow'
# ---- Load Tmux Plugin Manager and the plugins ------------------------------
# Plugin manager
set-option -g @plugin 'tmux-plugins/tpm'
# List of tmux plugins
set-option -g @plugin 'christoomey/vim-tmux-navigator'
# Initialize Tmux Plugin Manager (keep this at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
References
-
Config files
- .tmux.conf by Dreams of Code
-
YouTube
- Tmux has forever changed the way I write code by Dreams of Code
- Tmux will skyrocket your productivity - here’s how by typecraft
- Making Tmux better and beautiful - here’s how by typecraft
- How I use Tmux with Neovim for an awesome dev workflow on my Mac by Josean Martinez
-
Lists