Harnessing the Power of tmux on Linux and macOS: A tmux Cheat Sheet

Harnessing the Power of tmux on Linux and macOS: A tmux Cheat Sheet

28.12.2023
Author: HostZealot Team
2 min.
1058

Tmux is a tool that many users may find extremely helpful when it comes to running programs on a server. Tmux is a multiplexer tool. It allows to launch and manage multiple terminals from a single terminal.

Besides that, the program prevents programs from disconnecting by accident as they aren’t attached directly to the main terminal.

With all this, tmux is pretty fast and stable, which makes it extremely useful as soon as you learn well how to use it, what we are going to break down in the article that follows.

Exploring the Advantages of tmux

Tmux has a bunch of features that you can benefit from to effectively manage your Linus server environment. Among them:

Multitasking: With Tmux you can run simultaneously several programs within the same terminal. Tmux allows to split of the terminal into several separate terminals whereas the programs may run in the background.

Scripting and automation: Tmux opens a rich functionality with the ability to script and automate a variety of operations.

Configurability: Tmux offers a highly flexible configuration so you can coordinate the app’s functionality with very different requests.

Sessions, windows, and panes: Tmux offers a rather flexible, intuitive, and logical hierachic interface. A session is composed of windows, a window is composed of panes.

Plugins: Besides the essential functionality of Tmux, it can be done in many ways enriched with the help of external plugins.

Productivity: Managing dozens of sessions from just one window can find dozens of usages for projects of all types.

Efficient session management: With tmux you can feel free to detach and reattach to sessions at any moment without losing any progress.

Remote access: Tmux sessions can be accessed and shared across remote servers or different computers, making it a useful tool for remote collaboration and monitoring.

Collaboration: With tmux you can implement various approaches to collaboration, like pair programming.

tmux Installation Guide for Linux and macOS

In the following sections, we want to have a look at the installation of tmux on different systems as well as at different commands that will be very helpful when using the solution.

Installing on Debian-based Linux (e.g., Ubuntu)

To install tmux on Debian as well as Ubuntu, enter the following command:

sudo apt update && sudo apt install tmux

Installing on RHEL-based Linux (e.g., Fedora and CentOS)

Using Fedora, CentOS, and any other RHEL-based distro, you’ll have to use:

On CentOS and RHEL 7:

sudo yum -y install tmux

On RHEL 8 as wells as Fedora, Rocky, and AlmaLinux:

sudo dnf -y install tmux

Installing on Arch-based Linux (e.g., EndeavourOS)

For Arch Linux and other distros, that are based on it, you’ll need:

sudo pacman -S tmux

Installing on Alpine Linux

To install tmux on Alpine Linux:

sudo apk add tmux

Installing on macOS

One of the possible options of tmux installation on macOS is the use of Homebrew.

First, install Xcode:

xcode-select --install 

Next, install Homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After being installed, Homebrew can be used to install tmux:

brew install tmux

After the process has bee completed, verify the installation:

$ tmux -V
tmux 3.3a

Mastering tmux on Linux Distributions (Ubuntu, Fedora, CentOS, EndeavourOS)

Now that tmux is installed on whatever OS you use, it’s time to have a look at some essential commands and shortcuts that will be helpful to any tmux user:

Session Command
Function
tmux
New session
tmux new -s <name>
New session with the <name> of your choice
tmux ls
List of sessions
tmux a
tmux at
tmux attach
tmux attach-session

Attach to the most last used
tmux a -t <name>
tmux attach -t <name>

Attach to a session under the <name>
tmux kill-session -t <name>
Kill a session under the <name>
tmux kill-session -a
Kill/delete all sessions except for the current one
tmux kill-session -a -t <name>
Kill/delete all sessions except for the session under the <name>


Windows/Pane Commands
Function
tmux new -s mysession -n mywindow
Start a new session with the name mysession and window mywindow
Ctrl+b (
Move to previous session
Ctrl+b )
Move to next session
Ctrl+b & 
Close current window
Ctrl+b p
Previous window
Ctrl+b n
Next window
swap-window -t -1
Move current window to the left by one position

Navigating Key Bindings

Now let’s have a look at some of the tmux’ essential keyboard shortcuts for managing sessions and windows:

Keyboard shortcut
Function
Ctrl+b ?
List all the keyboard shortcuts
Ctrl-b d
Deattach from the current session
Ctrl+b %
Add a new pane to the right/split vertically
Ctrl+b “
Add a new pane to the bottom/split pane horizontally
Ctrl+b {
 Move the current pane to the left
 Ctrl+b }
Move the current pane to the right
Ctrl+b o
Switch to the next pane
Ctrl+b ;
Move between the current and previous pane
Ctrl+b >/<
Navigate between panes
Ctrl+b x
Close the current pane
Ctrl+b z
Toggle pane zoom
Ctrl-b c
Create a new window
Ctrl-b w
 Switch between sessions and windows

Harnessing the Power of tmux Commands

Using commands in tmux is identical to generally using commands in Linux distros. Let’s see how it is done on Ubuntu.

First, start tmux with the tmux command. This will start the tmux server, open session #0 in the same window, and attach to it.

Then you might need to de-attach from the session. Press Ctrl+b, then d. To attach once more, use tmux attach -t <name>.

To start a session with a certain name, type tmux new -s <named>. To list sessions, apply ls.

Delete a session with tmux kill-session -t <name>. Use either the name of the session or also its index.

To create many panes at once, use Ctrl+b %.

If you want all the sessions that are currently running to be displayed, use tmux ls.

After de-attaching from a session, you can re-attach to it again with commands tmux a, tmux at, tmux attach, or tmux attach-session.

You can also switch between the next and the previous session with the Ctrl+b command.

Another way of using this command is to append % after it, which will split a pane horizontally. Instead, for vertical splitting, add “ after the keyboard shortcut. Adding } will move the pane to the right and { to the left respectively.

​Command mode

Command mode is the mode that will allow you to operate with tmux sessions, windows, as well as panes. To enter it, append the colon sing [:] after the Ctrl+b command. After this, a command prompt will open in the lowermost part of the screen. After this, you’ll be able to use a variety of commands to control your interface.

tmux new -n mywindow is a typical command used for creating a new window. However, in the command mode, it’s enough just to type new -n mywindow. You can always switch to the command mode to make running commands easier.

The command mode can be used to synchronize tabs, that is, to make them perform the same tasks simultaneously. To enter it,  type setw synchronize-panes in the command mode. Afterwards, the command inserted in the command line of the current pane will be applied to all panes.

If you don’t remember some of the keyboard shortcuts, you can also refresh the list with Ctrl+b appending ? after the keyboard shorcut.

Conclusion

Tmux is a multifunctional tool that allows you to run several terminals from one single terminal window, which can be helpful in a variety of scenarios. In the article above we’ve provided some introductions into installing and usage of the tool. Now you should be able to perform a variety of operations with the help of the tool. In case you forget something, feel free to go back to this article and have a look at the cheat list provided. 

Related Articles