I am fairly familiar with Linux, I’ve been using different distros for some years now and have done some config editing here and there. I am also a web developer and use the terminal quite a lot and so I always stumble on people’s recommendation to use tmux and how good it is, but I never really understood what it does and, in layman’s terms, how can it be useful and for what use cases.
Can you guys please enlight me a bit on this?
Thank you.
Edit: if my phrasing is a bit awkward or confusing I apologize since I am not an English native speaker. (Maybe that’s why I never fully grasped what tmux is from other explanations xD)
Edite: Ok, just to clarify, my original struggle was to understand what made tmux different from using some terminal app and just split the screen xD
The most common use case for tmux
is to put long running terminal apps in the background when working remotely, e.g.:
# start a new virtual terminal
tmux
# do something in the terminal that will take a long while to complete
sleep 1m
# put the virtual terminal into the background
Ctrl-b d
# do other stuff
# reattach to the virtual terminal
tmux a
This allows you to disconnect from the server, but keep the process running in the background. It can also do split screen with multiple terminals, provide a scroll back buffer (Ctrl-b PageUp
) and other stuff. But using it for background processes is probably the most common one.
Tmux is no different from a terminal app that split the screen in terms of “multi window” functionality. However it’s not a graphical software, so you can start it remotely (eg. over ssh), and detach/reattach to it later without loosing what you where doing.
Probably somebody can provide a better answer, but for me tmux is useful due that it has session manager (really useful if your remote connection drops) and the ability to split the screen in multiple screens (usually I split vertical, but you can create easily 4x4 screen).
The only trick is the learning curve of the actions (usually ctrl + b and the key required). For example to split the window vertical, you must do ctrl + b and then %.
But as I said, probably you will get better and more technical answers _U
EDIT: some grammar mistakes.
I guess the best analogy is a “virtual desktop” but for the terminal.
It’s is a program which runs in a terminal and allows multiple other terminal programs to be run inside it.
Each program inside tmux gets its own “page” or “screen” and you can jump between them (next-screen, previous-screen etc).
So instead of having multiple terminal windows, you only have one and switch the screen/page inside it.
You can detech from the program and leave it running - so next time you log on to the server, you can re-attach to it and all your screens/sessions are still there.
Not super useful on your local machine - but when you have to connect to a remote server (or several) is really shines. Especially if you have to go through a jumphost. You can just connect to your jumphost, start tmux, then create a “screen” for each server you need to connect to - do your stuff and deattach. Next time, just re-attach and all your stuff is there.
Did that help?
Ok, now I guess I am seeing the value of it, specially with the “virtual desktop” analogy and the remote scenario, since I need to do some of it at work and having everything as I left it last time will be nice. Thank you!
Imagine you ssh into a server to do a database import, and from previous experience you know it will take about 3 hours. You start the restore, then get up to make dinner. You come back an hour later and realize you forgot to plug your laptop in.
Is the import command still running? Who knows.
With tmux you just charge your laptop, ssh in again, and reconnect to the virtual term that was running the command to check.
plus, if you disconnect in the middle of a command execution it doesn’t get killed (very important for system updates for example)
…. or if you get disconnected by, say, dodgy internet connection or such.
Ugh I remembering learning upstart and getting decent with it and then everyone went “nope, systemd”
Let’s just improve what we have and not change the whole goddamn thing again. That’s more annoying.
Also, some of the people on hackernews are so cringy. Like, dude we get it there is a bad default. Make your case and stop being a total jerkoff, because no one is going to listen to that guy and I bet that’s like 20% of the reason the other übernerds are digging their heels in about changing it.
Also fuck systemd 😅
I actually get a lot of use out of it locally. I usually have multiple sessions for different concerns and prefix + s lets me switch between them quickly using vi keybindings. I can even do prefix + w to switch to a specific window in a different session.
I don’t use vscode much lately, but when I did it was also useful sometimes to have the same window in my terminal client on one desktop and in vscode’s terminal on another when switching back and forth a lot to see a browser or database client or whatever. Just having the freedom to move the session around to different applications is nice.