Tmux is your good friend#

What is tmux?#

Tmux is a terminal multiplexer. It allows you to create several pseudo-terminals from a single terminal. It is useful for running multiple programs with a single connection, such as when you are connecting to a machine using SSH.

Main concepts#

Tmux has three levels: sessions, windows, and panes. A tmux session is a persistent terminal environment that can contain multiple windows and panes, allowing you to run and manage several processes in a single terminal window. Tmux sessions can help you manage multiple projects, eg, different sessions can be created for different projects.

Common session commands are as follows.

tmux new -s afm # create a new session named 'afm'
tmux new -s paper_writing # create another session for writing a paper.
tmux switch -t paper_writing # switch to the session 'paper_writing'.
tmux switch -t afm # switch to the session 'afm'.

Even if you turn off the terminal, you only use

tmux attach -t afm # reattach the session 'afm'

or

tmux attach -t 0

Then, all windows, commands, and processes will restore the state where you were last time. Note: Here,0 is the index of the session you want to reattach.

Some other useful session commands are:

tmux ls # list the sessions
tmux kill-session -t mysession # kill the session named 'mysession'
tmux rename-session -t old new # Rename the old session to a new session name.

How to save and restore sessions?#

  1. First, install TPM, Tmux Plugin Manager. Requirements: tmux version 1.9 (or higher), git, bash.

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
  1. Put this at the bottom of ~/.tmux.conf:

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'github_username/plugin_name#branch'
# set -g @plugin 'git@github.com:user/plugin'
# set -g @plugin 'git@bitbucket.com:user/plugin'

# Initialise TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
  1. Reload the TMUX environment so TPM is sourced:

# type this in terminal if tmux is already running
tmux source ~/.tmux.conf

TPM is installed. Then, hit prefix + I to fetch the plugin and source it. We should now be able to use the plugin.

# Fix colour unmatched
set-option -sa terminal-overrides ",xterm*:Tc"

set -g mouse on
set-option -g set-clipboard on

# Use vim keybindings
bind -r k select-pane -U
bind -r j select-pane -D
bind -r h select-pane -L
bind -r l select-pane -R

# Numbering starts at 1
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

# Force tmux to open new panes/windows in the current pane's directory
bind c new-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -v -c "#{pane_current_path}"


# Plugin list
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'nordtheme/tmux'
set -g @plugin 'tmux-plugins/tmux-resurrect' # Restore tmux environment after system restart.

# init TPM (must be at the end)
run '~/.tmux/plugins/tpm/tpm'

The plugin tmux-resurrect is used to realise restoring the tmux environment. The keys for saving and restore the environment is Ctrl b s/r.

How to start a new panel with the current path?#

Add the following two lines in the configuration file ~/.tmux.conf

bind '"' split-window -v -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"

Then update the configuration with this command

tmux source-file ~/.tmux.conf

Useful commands#

prefix + %: Split the window vertically
prefix + ": Split the window horizontally
prefix + z: Maximise the current pane

How to copy a piece of text in a tmux pane?#

Shift (Option in Mac) + (selection text)  # Select and Copy
Command + V  # Paste 

References#