---
title: "Terminal: Tmux"
description: "Notice: This is a little \"terminal treat\" – a short tip rather than a whole article. Enjoy!"
date: 2025-04-17
canonical_url: "https://christofersandin.com/texts/terminal-tmux"
section: "Texts"
---

#  Terminal: Tmux 

**Notice: This is a little "terminal treat" – a short tip rather than a whole article. Enjoy!**

**A way to standardize the window and session management inside the terminal.**

> tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they  
> keep running in the background) and reattach them to a different terminal.

I’m relatively new to using [Tmux](https://github.com/tmux/tmux/wiki) in my terminal sessions, since the main argument  
used to be that you can reconnect to all of the servers you work on from different machines. That was a use case that  
didn’t resonate with me since I spend most of my time on my machine.

However, 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](https://github.com/jimeh/tmuxifier), I can configure the layouts I need for work  
once and then attach to them whenever I switch contexts. This makes the startup time close to zero and has made my  
life easier.

I also like that I can jump around between terminal emulators like [iTerm2](https://iterm2.com/),  
[Ghostty](https://ghostty.org/), and [Wezterm](https://wezterm.org/) and have the same setup in all of them.

After a `brew install tmux` we can:

- `tmux` - start
- `tmux ls` - show sessions
- `tmux attach` - attach to old session
- `tmux attach -t name` - attach to `name` 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.

```shell
┌────────────────────────────────────────────────────┐   ┌────────────────────────────────────────────────────┐   ┌────────────────────────────────────────────────────┐
│                     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> $`
- Destroy a session: `<Prefix> :kill-session`

### 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`

```sh
# ---- 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](https://github.com/dreamsofcode-io/tmux/blob/main/tmux.conf) by Dreams of Code

### YouTube

- [Tmux has forever changed the way I write code](https://www.youtube.com/watch?v=DzNmUNvnB04&t=2s) by Dreams of Code
- [Tmux will skyrocket your productivity - here’s how](https://youtu.be/niuOc02Rvrc?si=uJyYE9WdfhjBs0gp) by typecraft
- [Making Tmux better and beautiful - here’s how](https://youtu.be/jaI3Hcw-ZaA?si=MacWaIT1wbhPy34k) by typecraft
- [How I use Tmux with Neovim for an awesome dev workflow on my Mac](https://youtu.be/U-omALWIBos?si=zSTINo9iecRUkMxo)  
    by Josean Martinez

### Lists

- [Awesome Tmux](https://github.com/rothgar/awesome-tmux)

### Guides

- [Make tmux Pretty and Usable](https://hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/) by Ham Vocke – a `.tmux.conf` walkthrough, line by line
- [The Tao of tmux](https://tao-of-tmux.readthedocs.io/en/latest/) by Tony Narlock – a free book for when you outgrow the intros
- [Smart tmux sessions with sesh](https://www.joshmedeski.com/posts/smart-tmux-sessions-with-sesh/) by Josh Medeski – session management with zoxide and fzf

---

## Metadata

  [\#terminal-treats](/writing/terminal-treats)

 Published .  
 Last modified .

 10 14 9 1109
