Last updated on November 23, 2020 by Dan Nanni
The introduction of the mouse was a wonderful innovation in making computers more accessible to average people. But for programmers and sysadmins, moving our hands off the keyboard while working on a computer can be distracting.
As a sysadmin, I spend most of the time working in the terminal environment. Opening tabs and moving around windows through multiple terminals slows me down, and I just can't waste any second when something is going really wrong with my server.
Tmux is one of those tools that are essential for my daily work. With Tmux, I can create complex development environments, and have SSH connections side by side. I can create multiple windows, split panes, attach and detach sessions, etc. After mastering Tmux, you will throw out your mouse away ( just kidding don't do it :D ).
terminator, Tmux (short for "Terminal Multiplexer") lets us launch multiple terminals in a flexible layout on a single screen, so that we can work with them side by side. For example, on one pane we can edit some config files with Vim, while on the other we are using
irssi to chat and on other pane, tailing some logs. Then open another window to update your system, and another to SSH to some servers. Navigating through them is just as easy as creating all these windows and panes. It is perfectly configurable and customizable so it can become an extension of your mind.
You can install Tmux by compiling it from sources or via your operating system package manager. I recommend you to use a package manager. It's faster and easier than compiling.
$ sudo apt-get install tmux
RHEL/CentOS require EPEL repo
$ sudo yum install tmux
$ sudo pacman -S tmux
The best way to use Tmux is working with sessions, so that you can organize your tasks and applications into different sessions the way you want. If you need to change a session, whatever runs inside the session won't stop or get killed. Let's see how it works.
Let's start a new session named
session, and run
top command in it.
$ tmux new -s new session $ top
CTRL-b d to detach from this session. To re-attach to it:
$ tmux attach-session -t session
And you will see
top still running in the re-attached session.
Some commands to manage sessions:
$ tmux list-session $ tmux new-session <session-name> $ tmux attach-session -t <session-name> $ tmux rename-session -t <session-name> $ tmux choose-session -t <session-name> $ tmux kill-session -t <session-name>
Often you will need to run multiple commands and perform different tasks in a session. We can organize all of them on multiple windows in one session. A window can be seen as a tab on modern GUI terminal (such iTerm, or Konsole). After configuring our default environment in a session, we will be able to create as many windows as we would need within the same session. Windows, like our apps running in sessions, persist when we detach from the current session. Let's check out an example:
$ tmux new -s my_session
This will create a new window and move focus into it. Now you can start up another application in the new window. You can write down the name of your current window. In this case I was running
top so that's the window's name.
To rename it, just press:
The status bar changes to let you rename the current window.
Once we create multiple windows in a session, we need a way to move through them. Windows are organized as an array, so every window has a number starting at
0. To jump quickly to other windows, press:
CTRL-b <window number>
If we have named our windows, we can look for them with:
and to list all windows:
and to move to a different window one by one:
CTRL-b n(go to the next window)
CTRL-b p(go to the previous window)
To leave a window, just type exit, or press:
You have to confirm if you want to kill off the window.
Sometimes you need to type in your editor and check a log at the same time, and having your editor and
tail side by side is really useful. With Tmux, we can divide a window into multiple panes. So for example, we can create a dashboard to monitor our servers and a complex development environment with the editor, the compiler and debugger running together side by side.
Let's create another Tmux session to work with panes. First let's detach from any Tmux session in case we are in a running session, with:
Start a new session named
$ tmux new -s panes
You can split a window horizontally or vertically. Let's start horizontally by pressing:
Now you have two new panes. Now vertically by pressing:
and now two more:
To move through them, press:
CTRL-b <Arrow keys>
I hope this tutorial has been helpful to you. As a bonus, tools such as Tmuxinator or Tmuxifier can streamline the process of creating and loading Tmux sessions, windows and panes, so that you can configure Tmux easily. Check them out if you haven't.