Taking into consideration the UNIX philosophy (“Make each program do one thing well.”), the terminal multiplexer expands on this idea by doing several things quite well, giving the operator additional console agility and swiftness.
In order to formulate this activity better, multiplexing represents a process used to send multiple data streams over a single channel or medium which is called a multiplexer.
One common use case would be a phone line carrying several active phone calls over a single wire where the multiplexer is the central switching box or, better yet, the mainstream introduction of DSL (somewhere during the 90s) made it possible to have an active phone call and uninterrupted internet connection - over the same line.
So how does this reflect to the current topic?
A terminal multiplexer does exactly the same thing as the before mentioned cases. Shifted into the world of terminals, it creates a single session pid on one host and allows multiple sessions to be displayed and managed on a single screen. If one of the connected clients drops/detaches, the session is kept alive since a SIGHUP (“signal hang up”) is never sent. This makes it possible for the client to reconnect/attach to the previously running session.
Imagine working on a long-running task over SSH and suddenly the connection drops. In most cases you’ll need to start from scratch or dig through logs in order to understand what occurred while the network was down. If a multiplexer was instantiated, you would simply attach to the running session and continue working where you left off.
Another good example would be (yet again) a long-running task, that takes an incredible amount of time to finish. If working remotely or on a laptop, you probably don’t want to keep your devices running for as long the task is ongoing. The nice possibility here is that you can initiate the task, detach from the session and log in periodically to check up on the running status.
Of course if the server crashes, the session state will be the least you’ll worry about:)
Continuing this post, I’d like to point out two of the most popular terminal multiplexers - GNU screen and tmux. Both battle tested with GNU screen being the oldest (multiplexing since ‘87) and tmux as a more modern implementation. Worth to mention that both projects are actively maintained and the GNU screen package can be found on almost all linux distros (MacOS too).
Each use a combination of a prefix + key command. A prefix, in this case, is just a grouped key combination. GNU screen uses Ctrl+a and tmux Ctrl+b. By referring to a prefix, I mean a pair of keys for each respective terminal multiplexer.
Both use a similar command for starting a new session. GNU screen has screen and tmux uses tmux in the console.
Detaching from the session is identical between the two. On GNU screen it’s prefix+d and on tmux is prefix+d.
Attaching to the session is shockingly different! GNU screen uses screen -r while tmux uses tmux attach.
So, taking into consideration the similarities between the two, a cheat sheet is needed in order to have a clearer understanding of these tools. The good part is that getting experienced with one, makes it easier to operate the other.
I’ll divide this cheat sheet into two sections: commands to do in the host terminal and those in the running session.
In the terminal:
|create a new session||screen||tmux|
|create a new named session||screen -S session-name||tmux new -s session-name|
|attach||screen -r||tmux attach|
|attach a specific session||screen -dr session-name||tmux attach -t session name|
|list available sessions||screen -list||tmux list-session|
Inside the session:
|create new window||prefix+c||prefix+c|
|navigate the output buffer||prefix+[||prefix+[|
|**select, copy, paste||space, enter, prefix+]||Ctrl+space, Ctrl+w, prefix+]|
|exit from the buffer navigation||q||q|
|zoom/unzoom pane in window||n/a||prefix+z|
|close pane||n/a||prefix+x or type exit|
|split vertically||prefix+l (not on some MacOS versions)||prefix+%|
|switch active windows||prefix+[0-9]||prefix+[0-9]|
|detatch from a session||prefix+d||prefix+d|
|see key bindings||prefix+?||prefix+?|
**Copy and paste? It’s a bit unusual at first, but the same concept is shared between both multiplexers.
In order to copy and paste, we first need to get into the “navigate the output buffer” mode (check the table above). In that mode it’s possible to scroll all over the available buffer. In GNU screen, pressing space will invoke the selection process. Using the key arrows, we can choose what text we would like to copy. Pressing the enter key the selected text is stored temporarily in the memory. Using prefix+] will paste the selection back to the screen. Tmux has a fairly similar approach. The same key binding is used for navigating the output buffer, Ctrl+space is used for text selection and Ctrl+w for copying and the same keys are used for pasting back the text.
Wondering what else could a multiplexer do? How about downloading big files? Let’s say there’s a new GNU Linux distro you’d like to download but don’t want to wait for the large .iso file to transfer in order to continue your work in the terminal. This could be the perfect moment for GNU screen to shine!
$ screen -dm wget http://ftp.nluug.nl/ftp/pub/os/Linux/distr/puppylinux/puppy-bionic/bionicpup32/bionicpup32-8.0-uefi.iso
What we’re doing here is creating a new session and using wget to download Puppylinux, a fairly small GNU Linux distro. By attaching to the session, we can see that the process is ongoing and after the transfer is done the process will exit.
Even though this can be done by manually creating a session and running the command, the chosen approach makes it very convenient for automated processes ie. a cleanup or archiving script.