Getting started with tmux

Getting started with tmux

What is tmux and why should I use it?

Tmux is a terminal multiplexer and was originally written in 2007. It's free software and available at their github page here. https://github.com/tmux/tmux. Terminal multiplexers provide several functions.

First, it allows you to turn a single terminal sessions into multiple sessions all on the same terminal emulator window. This is why I use tmux because it lets me run a dozen programs at the same time in a very easy-to-navigate manner. Previously I was struggling to juggle all the windows around on my desktop. I found that tmux provided me a keyboard-shortcut-based workflow to increase my productivity.

Second, it allows you to continue your session if you disconnect. Typically when you disconnect from an SSH session, your shell session is lost even if it's just a momentary blip in connectivity. With tmux your shell session is preserved and the running programs won't even know it happened.

Installation

Installation is straight forward. Install it from your system's package manager. I use MacOs primarily so I install it via brew like this.

brew install tmux

Specific installation instructions for different operating systems can be found here. https://github.com/tmux/tmux/wiki/Installing

Starting tmux

To start tmux, just type:

tmux

The Prefix Key

While you can use the mouse to perform some functions if you want, you will send most of the commands via keyboard shortcuts. Your computer knows you're entering a tmux command when you hit the prefix buttons. You can configure this to whatever you want, but by default it's Control + b.

Panes

Lets start by splitting the pane and creating a second terminal session in the same window. You can do this by hitting Control+b " . This will create a horizontal split in the terminal

You can type exit to close the active pane or Control+b x .

Likewise to open a new pane but split vertically, type Control+b %

You can also split panes multiple times in multiple directions.

To navigate between panes hit Control+b then use the arrow key to go the direction you want. For instance to go to the pane to the right hit Control+b Right.

Zooming

Sometimes when you're working on something, you need to focus on one pane for a minute, but you don't want to close the other panes. You can make your active pane fill the terminal by typing Control+b z. You can restore the pane back to it's original size by typing Control+b z again.

Windows

Sometimes you want to work on something else, but either your screen is already full of panes. In this case you can open a new window in the session. I often have to work with multiple repositories at the same time and will open one window per repository.

To create a new window, type Control+b c

You'll notice that the window number and what command is being run is listed in the bottom left. The first window is marked 0:zsh* and the second is 1:zsh*

To switch between windows you can use the keys Control + b n to go to the next pane or Control + b p to go the previous pane.

You can also view a list of all the open windows and see a preview of each by running Control + b w . You can either hit enter to jump to that window or you can quit by typing q .

Sessions

So far we've run everything within a single instance of tmux running. You can create additional instances called sessions.

To start a session all you have to do is type tmux in your terminal. Once you are in a running session you can type Control + b d to detach. This will exit your terminal session and put you back at the terminal. To see a list of sessions run:

tmux list-session

To reattach to the session run

tmux attach -t <session name or number>

To delete a session, run

tmux kill-session -t <session name or number>

Customization

It may be helpful to customize tmux. The advantage of this is you get it to behave exactly as you want, but the drawback is you don't have access to your custom config if you are using tmux on a different computer. By all means go ahead and change your config, but you need to know how to manage at least the basics with default key bindings.

Tmux stores it's config inside the text file ~/.tmux.conf. I personally like to use vi keybindings whenever possible so I added moving around with vi keys. I also mapped resizing panes to the same keys but while holding down control. Additionally, I enabled mouse support. My config now looks like this:

set -g mouse

# vim-like pane switching
bind -r k select-pane -U
bind -r j select-pane -D
bind -r h select-pane -L
bind -r l select-pane -R

# vim-like pane resizing
bind -r C-k resize-pane -U
bind -r C-j resize-pane -D
bind -r C-h resize-pane -L
bind -r C-l resize-pane -R

You will need to kill all existing tmux sessions for the changes to take effect.

Plugins

The last customization is the world of tmux plugins. These are third party add-ons that extend the functionality of tmux. The easiest way to get started with this is to use the Tmux Plugin Manager (TPM). The repo is located here: https://github.com/tmux-plugins/tpm

You first need to clone the repo. I like the non-standard location inside ~/.config but you can place it wherever you want.

git clone https://github.com/tmux-plugins/tpm ~/.config/tmux/plugins/tpm

We'll also be installing and configuring a tmux theme. As with all applications, the best theme to use is catppuccin (https://github.com/catppuccin). So we'll be adding that plugin and configuring some of the display as well.

You'll need to add some things to your ~/.tmux.conf file. It should look like this now.

set -g mouse

# vim-like pane switching
bind -r k select-pane -U
bind -r j select-pane -D
bind -r h select-pane -L
bind -r l select-pane -R

# vim-like pane resizing
bind -r C-k resize-pane -U
bind -r C-j resize-pane -D
bind -r C-h resize-pane -L
bind -r C-l resize-pane -R

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'catppuccin/tmux'

set -g @catppuccin_flavour 'macchiato' # latte or frappe, macchiato, mocha
set -g @catppuccin_status_left_separator  ""
set -g @catppuccin_status_right_separator " "
set -g @catppuccin_status_right_separator_inverse "yes"
set -g @catppuccin_status_fill "all"
set -g @catppuccin_status_connect_separator "no"
set -g @catppuccin_window_left_separator " "
set -g @catppuccin_window_left_separator_inverse "yes"
set -g @catppuccin_window_fill "all"
set -g @catppuccin_window_connect_separator "no"

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.config/tmux/plugins/tpm/tpm'

Make sure you have no active sessions open and launch tmux

Now type Control + b I

This will download, load, and configure the theme. It should look like this.

There are a ton of other helpful plugins out there that are worth investigating.

Conclusion

There are still quite a few more commands you can learn that will enhance your tmux experience, but most people really learn by doing and getting some commands committed to muscle memory. Launch tmux and play around with it for a while. When you are ready to try some additional commands, I recommend checking out https://tmuxcheatsheet.com/.