Installing Tmux in Linux

Tmux is available in the official repositories of most Linux distributions.

On Arch Linux and its variants, run the following command to install it.

$ sudo pacman -S tmux

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install tmux

On Fedora:

$ sudo dnf install tmux

On RHEL and CentOS:

$ sudo yum install tmux

On SUSE/openSUSE:

$ sudo zypper install tmux

Installing tmux on MacOs:

Pre requisite (Install homebrew):
Run in Terminal app:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null


brew install tmux

Creating named sessions

If you use multiple sessions, you might get confused which programs are running on which sessions. In such cases, you can just create named sessions. For example if you wanted to perform some activities related to web server in a session, just create the Tmux session with a custom name, for example “webserver” (or any name of your choice).

tmux new -s session_name

List Tmux sessions

To view the list of open Tmux sessions, run:

tmux ls

Attaching to Tmux sessions

You can attach to the last created session by running this command:

tmux attach

Or,

tmux a

If you want to attach to any specific named session, for example “ostechnix”, run:

tmux attach -t ostechnix

Or, shortly:

tmux a -t ostechnix

Kill Tmux sessions

When you’re done and no longer required a Tmux session, you can kill it at any time with command:

tmux kill-session -t ostechnix

To kill when attached, press Ctrl+b and x. Hit “y” to kill the session.

You can verify if the session is closed with “tmux ls” command.

Split Tmux Session Windows

Tmux has an option to split a single Tmux session window into multiple smaller windows called Tmux panes. This way we can run different programs on each pane and interact with all of them simultaneously. Each pane can be resized, moved and closed without affecting the other panes. We can split a Tmux window either horizontally or vertically or both at once.

Split panes horizontally

To split a pane horizontally, press Ctrl+b and ” (single quotation mark).

Split Tmux pane horizontally

Split Tmux pane horizontally

Use the same key combination to split the panes further.

Split panes vertically

To split a pane vertically, press Ctrl+b and %.

Split Tmux panes vertically

Split Tmux panes vertically

Split panes horizontally and vertically

We can also split a pane horizontally and vertically at the same time. Take a look at the following screenshot.

Split Tmux panes

Split Tmux panes

First, I did a horizontal split by pressing Ctrl+b “ and then split the lower pane vertically by pressing Ctrl+b %.

As you see in the above screenshot, I am running three different programs on each pane.

Switch between panes

To switch between panes, press Ctrl+b and Arrow keys (Left, Right, Up, Down).

Resize panes:

// This assumes that you've hit ctrl + b and : to get to the command prompt
:resize-pane -L

Here are some additional tmux pane resizing examples:

:resize-pane -D (Resizes the current pane down)
:resize-pane -U (Resizes the current pane upward)
:resize-pane -L (Resizes the current pane left)
:resize-pane -R (Resizes the current pane right)
:resize-pane -D 10 (Resizes the current pane down by 10 cells)
:resize-pane -U 10 (Resizes the current pane upward by 10 cells)
:resize-pane -L 10 (Resizes the current pane left by 10 cells)
:resize-pane -R 10 (Resizes the current pane right by 10 cells)

Scrolling T-mux panes:

Ctrl-b then [ then you can use your normal navigation keys to scroll around (eg. Up Arrow or PgDn). Press q to quit scroll mode.

Alternatively you can press Ctrl-b PgUp to go directly into copy mode and scroll one page up (which is what it sounds like you will want most of the time)

In vi mode (see below), you can also scroll the page up/down line by line using Shift-k and Shift-j (if you’re already in scroll mode). Unshifted, the cursor moves instead of the page.

Excerpts from the man page:

tmux may be controlled from an attached client by using a key combination of a prefix key, ‘C-b’ (Ctrl-b) by default, followed by a command key.

 The default command key bindings are:

[           Enter copy mode to copy text or view the history.

Function                     vi              emacs
--------                     --              -----
Half page down               C-d             M-Down
Half page up                 C-u             M-Up
Next page                    C-f             Page down
Previous page                C-b             Page up
Scroll down                  C-Down or C-e   C-Down
Scroll up                    C-Up or C-y     C-Up
Search again                 n               n
Search again in reverse      N               N
Search backward              ?               C-r
Search forward               /               C-s

Plus a bunch more. Note that you have to press C-b twice if you use that for page up since C-b is bound as the command key. See the man page for information on prefacing a copy mode command with a repeat count.

You can set the key binding mode using Ctrl-b, then

:set-window-option mode-keys emacs

or vi.

Source:
https://linux.die.net/man/1/tmux

https://linuxize.com/post/getting-started-with-tmux/