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!

  • @[email protected]
    link
    fedilink
    210 months ago

    Nice introduction!

    Modify your terminal shortcut to directly invoke tmux. Tmux lets you group terminal windows into sessions which can be easily inspected; ctrl+b s. Tmux is therefor my workspace for terminals. I use the fastest terminal - for me it is st. Tmux enables me to have an unlimited scrollback buffer; But usually one pane history is for a specific task. So my scrollback buffer is my history for specific programs.

    Attaching a remote session from within tmux prevents wrong input to the remote machine.

    Maybe someone is interested.

    • @PlutoPartyOPM
      link
      2
      edit-2
      10 months ago

      I just spent a little bit checking out suckless.org. I had heard of st before, but never really gave it any serious thought. I’ve been using terminator for a very long time. I’ve liked it because of how easy it is to split and add more terminals. That makes it easy to monitor a lot simultaneously. terminator is mainly why I’ve never taken the time to memorize or use the panes and windows shortcuts with tmux. However, I think I’m a new fan of st now that I’m looking at it and just tried it out. Just a stupid simple terminal that works. Love it. The ability to split terminals with terminator is a little redundant when I end up attaching to tmux anyway. Plus, I’d save time just keeping my views persistent with tmux instead of pulling each one up independently. That does get annoying. Not to mention all the other features of terminator are way too much bloat for me. st + tmux combo looks perfect and I think I am going to give it a fair shot as my primary daily driver.

      Thanks for sharing. Gaining insightful info like this is the whole reason I’m taking the time to write such posts. Now I might be sucked (hehe) into trying out their windows manager, too.

      • @[email protected]
        link
        fedilink
        2
        edit-2
        10 months ago

        I really, really like your writing style. Keep it up!

        Glad that you tried it! I could never describe my setup in such a nice text.

        Make sure you replace the terminal startup with st -e tmux. This was the most important bit for me. (Edit: you can detach instead of closing a terminal. This way you can revisit it later, until you are truely done with the task at hand.)

        Also, you can rename windows und sessions. Given that my current tmux runs for over 100 days, it is quite handy over time!