How tmux sparks joy in your Linux terminal

Organize your terminal like Marie Kondo with tmux.
152 readers like this.
Linux keys on the keyboard for a desktop computer

Let me set the scene: One day, I was debugging a time intensive feature generator process. I had a terminal open on my Mac, and I was connected in to my remote Linux virtual machine (VM) where the process would be initiated. I checked a few things with the script, and everything looked good. But by the time I kicked off the process, it was just about time to leave the office for the day. Closing my laptop would kill my shell, which would kill the process. The process takes forever and, instead of just being able to debug the results when I got home, I'd have to start it back up and wait for it to finish. I started to come to terms with the fact that I'd have to stay late and was not happy about this turn of events.

A coworker noticed my frustration and asked how he could help. I explained my issue, saying that there had to be a way to continue running the process even after I closed the terminal since it was on a remote VM. I was worried that it would have to be connected to my shell to not die. Then he introduced me to tmux.

Running scripts remotely with tmux

To begin, I did a yum install of tmux. Then I started a tmux session by typing tmux as a command. I could do everything in this tmux session that I would do in my normal shell. Since I had just started the process, I killed it in the original terminal. Then I went over to the tmux session and started the script again to run the process. Finally, I put my laptop to sleep like a typical end to my workday.

When I brought my laptop back up, my terminal shell had disconnected from my VM as I expected. I logged back into the VM on the shell, except it was blank like a shell normally is when you just start it up. My initial reaction was panic, but then I learned I just needed to re-access my tmux session since it just hums along in the background.

To re-attach to my tmux session, I entered tmux ls to list all the sessions I had open. There was only one, session 0. To go back into that session, I just had to do tmux attach -t 0. Lo and behold, the session came up, and my script was still running. I was ecstatic and quickly wanted to learn what else I could do with tmux.

Using tmux as a window manager

I never close browser tabs, and I never close windows, so I probably have about 20 or so terminals open and running at any point in time. Marie Kondo probably feels a disturbance in the force whenever I open a new window. But instead of having four terminals open that are all connected to the same VM and trying to keep track of them and where they are in my terminal list, with tmux, I can open one terminal to the VM and have four panes in that terminal. This really comes in handy. For example, I often start a process on the VM and then need to go and edit a properties file somewhere. Since the first process is still running, I open another terminal to edit the file. Then I realize I need to run a second process while tailing the log in another window to know how to edit the properties file I just opened. Without tmux, that would create three terminals that would never be closed.

tmux has especially been a lifesaver for running Docker and Kubernetes. It is incredibly helpful to dedicate a container to its own pane instead of opening a new terminal. The master pane allows me to monitor the health of my containers. I can easily open a new pane whenever I need to enter a container. I can even run docker-compose up and docker-compose down on every container at the same time if I want to and have just one terminal open while I do it.

The panes are already formatted and adjacent to each other, so I don't have to collect all my terminals that are connected into the one VM and try to drag and drop them next to each other on my monitor—only to have to do it all over again when I need to open a new terminal. tmux even lets you resize the individual panes on the fly! It's really easy to close a pane when you're done with it. You're not closing the terminal to the VM; you can just open a pane back up. This helps me keep my desktop clean instead of cluttering it up with forgotten terminals.

Remapping the tmux prefix

I recently learned that I can remap the tmux prefix to make things even easier. Before executing a tmux command (e.g., making a new pane, resizing a pane, or copying and pasting some text in your terminal), you have to input a specific key pattern to let tmux know that you're about to invoke a tmux command instead of any ol' Linux command. The default prefix is CTRL+b. For example, the command to open a new pane horizontally adjacent to your current one is just %, but for tmux to listen, you would have to input CTRL+b+%. CTRL+b is a little awkward for me, so I remapped it to the backtick (`). The remapped prefix makes things smoother than having to hit two keys that aren't super conveniently located. Now if I want a new pane I can just key in `+% and voila!

tmux from start to finish

The first thing I do on any new Linux VM is to install tmux. The first thing I do in any terminal connected to a Linux VM is to start a tmux session! I even take my base VM snapshots after I install tmux so I don't have to reinstall tmux if I need to revert to a snapshot. Anytime I log back into a Linux VM, I always do tmux ls to find my previous tmux session, then attach to it and clip right along where I last left off.

What to read next

12 Comments

I recently switched from ``screen`` to ``tmux`` and life's been 33% more enjoyable ever since.

I identify strongly with this article.

I'm very surprised that the above didn't write this article given the history of a particular presentation.

I've been swearing by tmux since I discovered it in a comparison of gnu vs. bsd tools.

In reply to by sethkenlon

You're running macOS. If I recall correctly, it comes with a Ruby interpreter...
Check out Tmuxinator next:

https://github.com/tmuxinator/tmuxinator

Just wondering...what can tmux do that screen can't? I've used screen for
years and really like the custom status lines I have created. What would
convince me to switch to tmux?

Not much, honestly. GNU screen was my main driver until one day I was about to give a talk at Linux Conf AU and realised I needed a "presentation mode" for my terminal. I wanted keep the terminal full screen, but also be able to flip back and forth between my notes. Tmux can do that: you can open tmux in one terminal window, have it on one screen (or computer, for that matter) and then attach to that same tmux session from another terminal - and anything you enter into one is displayed, in real-time, on the other.

Since then, I decided that for me that flexibility was pretty nice, so I've mostly switched to tmux (so I can get to know it better). But that may be a pretty niche use case, so that may not persuade you to use tmux over GNU Screen.

Then again, it doesn't have to be a wholesale switch from one to the other. They're both Free Software, they coexist peacefully. You can use one or the other as needed!

In reply to by carltm

Some would see that behaviour as a disadvantage of connecting to the same session: with "screen" multiple people can connect to the same session, but have different, independent views. With tmux, all is shared within a session.

To emulate the screen behaviour in tmux, you have to tell tmux to start a new session in the same session group as the original session. See the man page for tmux, "new-session -t".

In reply to by sethkenlon

I don't know if you do a lot with region/pane layout. In both tmux and screen, you can split panes/regions into sophisticated layouts. The difference is that tmux remembers it when you detach and later re-attach. Screen will forget the layout as soon as you detach. This can be pain if you use it for development and monitoring.

In reply to by carltm

Oh, also, tmux allows a lot of customisation and mouse interaction. I can now select and resize panes with the mouse, while also selecting text that is automatically copied to both the tmux paste buffer AND the desktop paste buffer.

In reply to by Steven Bakker

All Y'all seem to have completely missed the presence of byobu, a set of configuration files for GNU Screen and tmux that makes everything so much easier to deal with. It's been a part of ubuntu-server since 2012, and installable from other distros since about 2014. Try it, F9 is the key to bring up the help menu!

Nice, hadn't heard of byobu. I'll have to give it a go.

In reply to by Graham Cantin (not verified)

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.