Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.
How tmux sparks joy in your Linux terminal
How tmux sparks joy in your Linux terminal
Organize your terminal like Marie Kondo with tmux.
Get the newsletter
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 tmuxTo 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.