What is tmux?
tmux, short for terminal multiplexer, is a command-line tool that allows you to split your terminal into multiple panes, create and manage multiple terminal sessions, and attach and detach to them. It allows you to create multiple terminals in one window.
What's the point?
The feature I appreciate most is the attach and detach functionality. I manage a multitude of servers running an array of services and tasks. There are instances where my internet connection acts up while I'm working remotely and I get disconnected. Losing my shell environment and any ongoing processes due to a hiccup in my internet is frustrating and can be time consuming.
Working within a tmux session allows me to reconnect, attach to my session, and seamlessly pick up right where I left off without sacrificing any progress. Another nice thing is that I can effortlessly move to another workstation and resume my session on a remote machine there. I find it particularly useful to detach and send certain processes to the background. While testing and developing, I often choose this approach in lieu of setting up a systemd service. What many people will do is create a script that launches a service in a tmux session, which allows for attaching to it at some point later to either interact or view any output from what is running easily.
If it is hard to conceptualize the value of this, you can think of it as having many terminal windows open that you can minimize and come back to later.
Using tmux
There are a lot of tmux features and shortcut keys, but I'm going to cover the ones I regularly use that fit my needs. As with most things in Linux, there are several ways to accomplish the same task. Here is a handy cheatsheet of tmux commands. Try not to be overwhelmed with the amount of shortcuts. I certainly don't have them all memorized. If you're like me, you'll have just a few you regularly use and it'll become muscle memory after a short time.
Installing
Not all systems will have tmux installed by default, but it is very likely in the package manager as tmux
. sudo dnf install tmux
, sudo apt install tmux
, or sudo pacman -S tmux
will likely get the majority of people there, depending on which system and package manage is in use.
New Session
The most simple way to start a tmux session is to simply run tmux
. This will create the virtual session with a generic name. I prefer to name my sessions so I can easily identify them if I get several sessions going. This makes it easy to identify what is what. I start my sessions with tmux new-session -s sessionname
Detach Session
To detach from this session, you press Ctrl+b
then d
. That session is now running in the background and whatever process you were running, if any, are still running. You'd be free to end the ssh connection if that is how you are connected and that session will still remain alive.
Listing, Attaching and Switching Sessions
To list your running sessions while not in a tmux session, you can run tmux ls
. To attach to one of these sessions by name, you can run tmux a -t sessionname
. What I often do out of laziness after connecting to a machine is simply run tmux a
. This will join the last session you were attached to or sometimes it feels like it just selects a random one. From there, pressing Ctrl+b, s
will open a list of all the running sessions you can easily select and jump to with the arrow keys and pressing enter
. This is my normal workflow, but again, there are several ways to accomplish the same thing and even several variants of the commands, as can be seen on the cheatsheet link above.
Killing a Session
The usual way that I kill a session is to just run exit
while inside of the one I wish to end. As noted on the cheatsheet, there are other ways to kill it without actually entering it.
Scrolling
The only other common feature I use is scrolling. While inside a session, you lose the ability to easily scroll back as you normally would in a terminal emulator such as terminator or konsole. To scroll back up you use Ctrl+b [
. You can then use the arrow keys to scroll above what is presently on the screen. Ctrl+c
gets you out of scrolling mode. Be careful to only Ctrl+c
once, as you might unintentionally kill a running process if there is one going.
Conclusion
This is how I usually use tmux. It'll be useful to look at the cheatsheet and play around with some of the other features you might find handy, such as creating panes and windows.
Another popular multiplexer that accomplishes a lot of these same tasks is screen
. screen
is older but many still find it just as useful.
I'm curious how other people utilize tmux. Are there any lesser known tips or tricks that I didn't cover or may not know about? Better methods that'd make my workflow as described above easier? Does anyone have a strong opinion on using screen
instead of tmux
? Any considerations or dangers that people should be aware of when using a multiplexer? If so, let us know!