A tmux Guide!
I’ll be teaching you how to use tmux, a terminal multiplexer!
When I moved back to Wayland from X11, I started missing a program called tabbed from suckless.org, as my terminal foot did not have tabbing functionality built into it. So I started searching around for programs that offered tabbing and found tmux!
tmux is a terminal multiplexer that offers features such as multiplexing (obviously), tabs and session saving. I primarily use it to save my sessions between terminal closures and power shutdowns (more on that later!), as well as the tabs. Multiplexing is basically having the capabilities of a manual(!) tiling window manager inside of your terminal. I don’t use tmux’s ssh capabilities at all, so I won’t be covering that in this guide.
The Basics of tmux
Prefix Combo
First of all, you need to know what the prefix key is. The prefix key is kind of like a modifier key, just that you don’t hold it down while pressing a key combo. The default prefix key is Control+b. So for example, in order to open a pane on the bottom of your terminal, first press Control+b, then after releasing that press “.
However, I set the prefix key to Control+z because it’s just easier for me to hit the prefix combo. I use the 4th finger to hit control, so my hand can’t stretch far enough to hit b, so I rebinded it like this:
# rebind prefix
unbind C-b
set -g prefix C-z
bind C-z send-prefix
Other Essentials
You can enable mouse support via set -g mouse on
. I recommend enabling this if you’re not a hardcore keyboard fan, especially since moving to other panes might be slightly less efficient if you use a key combo.
Since I’m a hardcore neovim user :3, I make use of my keyboard a lot. (though I still use a mouse) You can enable vi mode in tmux via set-window-option -g mode-keys vi
.
Multiplexing
Multiplexing as I mentioned earlier is a way to utilize the power of a tiling window manager in your terminal! You can open panes to the right and bottom of your screen. Use prefix + "
to open a pane to the bottom and prefix + %
to open a pane to the right.
If you enabled mouse support earlier, you can click on panes to switch focus to them. However, if you want to use the keyboard to switch panes, I have you covered too! You can bind keys to implement vim style pane selection using h j k l.
# Vim style pane selection
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
Note: All of these keybinds must be entered after pressing the prefix combo.
Pressing prefix+& prompts for confirmation then closes the current window. All panes in the window are killed at the same time. prefix+x kills only the active pane.
Windows/Tabs
You can open a new window and move to it using the prefix+c combo, and prefix+{number} to switch to the specified window number. Annoyingly, by default tmux makes the default window start at 0, not 1, which makes it very annoying because 0 is at the opposite end of the keyboard from 1. To fix this, do this:
# Start windows and panes at 1, not 0
set -g base-index 1
set -g pane-base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on
By the way, if you want to rename your current window, use prefix+,
Sessions
Sessions are saved when you detach from tmux using prefix+d, NOT control+d. Doing the later will cause tmux to kill the session. You can start a new session with tmux new
and a new one with a name with tmux new -s <name>
. You can reattch to that session using tmux attach -t <name>
.
Sadly, tmux does not save your sessions when you turn your computer off. However, that can be fixed with a tmux plugin!
Plugin
First, in order to install a plugin, we need to install tpm, the tmux package manager. We can do this by doing the following steps:
- Clone the tpm repo onto your filesystem.
1git clone https://github.com/tmux-plugins/tpm ~/.config/tmux/plugins/tpm
- Put this line at the very bottom of your tmux config:
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.config/tmux/plugins/tpm/tpm'
- Reload the tmux config:
1# type this in terminal if tmux is already running
2tmux source ~/.config/tmux/tmux.conf
Now you’re all set to install tmux plugins!
To install a new plugin, add a new plugin to your tmux plugin with set -g @plugin ‘…’ Press prefix+I (capital i, as in Install) to fetch the plugin.
To uninstall a plugin, remove or comment out the plugin from the list. Press prefix + alt + u (lowercase u as in uninstall) to remove the plugin.
Here are some plugins that I use:
- tmux-sensible: This plugin installs some sensible configuration options for tmux that every user will want which you can see in the Github repo.
- tmux-resurrect: This is the plugin I was talking about earlier that allows you to save your tmux sessions once you shut your computer down. To save, press prefix+control+s. To restore, press prefix+control+r.
- tmux-autoreload: This plugin watches your tmux configuration file and automatically reloads it on change. It depends on the program
entr
, which is available on most distro’s repos, including the one I use, Alpine Linux.
There are 3 more plugins that I haven’t listed yet, because I’ll be talking about them in the following sections.
Colorscheme and Status Line
I use the Catppuccin colorscheme for tmux, because I like pastel colors :3 I have this in my config file:
set -g @plugin 'catppuccin/tmux'
# catppuccin theme
set -g @catppuccin_flavour 'mocha'
set -g @catppuccin_status_modules_right "gitmux application session"
This theme also changes the status line to a more pleasing look. I set one of the modules to gitmux for git info.
Copy mode
Copy mode can be activated by pressing prefix+[. This will make your cursor able to move around your terminal (even in empty whiteplace) and select items! Since I’m used to vi keybinds, I’ll use the plugin tmux-yank to copy stuff to the system keyboard. Also, I’ll add some keybinds for copy-mode in my tmux config:
# copy mode keybinds
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
So now when we enter copy mode, we can press the v key to enter visual (selection) mode just like in vi, or control+v to enter rectangular selection mode. Once you have an area selected, we can press y to yank the selected text to our clipboard! Note that you will need a clipboard such as wl-clipboard
if you’re on Wayland.
Conclusion
In conclusion, tmux is a really useful way to have tabs and multiple windows inside of a terminal window! I’ll like to thank Dreams Of Code for their tutorial on tmux, as most of my config is inspired by their video on tmux.