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

  • Phoenix3875@lemmy.world
    link
    fedilink
    arrow-up
    9
    ·
    edit-2
    2 years ago

    It’s a “terminal multiplexer”, i.e. you can start multiple terminals in a single terminal.

    You might ask, why not open a new terminal window or tab? Well, you can only do that in a desktop environment and that’s not always available. Even if you can, you might want the terminals to be side by side in a single screen, which might not be easy to do with window tiling.

    The real power of tmux, though, is that it manages the session you created. To quote from the manual:

    tmux may be detached from a screen and continue running in the background, then later reattached.

    So, one use case would be saving your current terminal setup. Instead of exiting the terminal and navigating to the project and setting up the environment again next time, you can simply detach and re-attach.

    When connecting to a remote server, this is especially useful:

    Each session is persistent and will survive accidental disconnection (such as ssh(1) connection timeout) or intentional detaching

    Suppose you want to execute a long running command on a remote server. If you just put it to foreground, when you exit the ssh session, the job is also killed. If you put it to the background, its output can’t be easily observed.

    With tmux, you can simply run it in the foreground like normal and detach. When you reattach later, the job is running and you get all the output easily, as if you have been in that session all along.

  • bionicjoey@lemmy.ca
    link
    fedilink
    arrow-up
    9
    ·
    2 years ago

    Decoupling the terminal from the shell also means you can keep your shell open while closing your terminal window. This can be useful on servers, or for long running commands that you want to be able to check on.

  • belshamharoth@lemmy.world
    link
    fedilink
    arrow-up
    8
    ·
    2 years ago

    To understand why you might want to use tmux try the following:

    1. Open your terminal
    2. Start editing a file with vim or nano but don’t save the file
    3. Close then re-open your terminal

    You will have lost your progress, next we can repeat but this time using tmux so you don’t lose your session:

    1. Open your terminal
    2. Start a tmux session using tmux
    3. Start editing a file again using vim or nano
    4. Close and re-open the terminal
    5. Type tmux a to re-attach to the existing session

    Note that this time none of your progress is lost.

    Aside from enabling you to have a persistent session, tmux also allows you to have multiple terminal panes open so you can do more than one thing at a time in the window, to see what I mean try this:

    1. Open your terminal
    2. Start a new tmux session using tmux
    3. Type top to begin listing processes
    4. Press ctrl b then % to make a new split pane
    5. Enter ls or other terminal commands

    You will see that you can use more than one panel to do things. This can be useful for example if you want to watch run tests and also run other commands.

    • teawrecks@sopuli.xyz
      link
      fedilink
      arrow-up
      2
      ·
      2 years ago

      All of this is way more useful when all you have to work with is a tty or an ssh session. No X? No problem.

  • Corngood@lemmy.ml
    link
    fedilink
    arrow-up
    6
    ·
    2 years ago

    I didn’t see this mentioned, but by far the thing I depend on tmux for the most is being able to quickly copy and paste text from the terminal. e.g. grabbing a file name from the output of git diff. How does everyone else do this?

    Another cool one is being able to attach to a session on my phone to check on something, and have it automatically resize without disconnecting my desktop.

  • Kurotora@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    2 years ago

    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.

  • PeterPoopshit@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    2 years ago

    People have already made lots of good replies but here’s my summary:

    tmux is a terminal multiplexer. It allows multitasking in command line only environments. For example if you have to do a sudo apt upgrade but don’t want to leave your ssh client logged in until it finishes, you can run it in a tmux session so it will happen in the background even if you’re not logged in.

    To start a new session, type “tmux”

    To view running sessions, type “tmux list-sessions”

    To switch to a running session, type “tmux attach-session -c N” where N is the number of the session.

    To exit a tmux terminal and go back to the main terminal, do ctrl+b and then press d.

  • Captain Aggravated@sh.itjust.works
    link
    fedilink
    arrow-up
    2
    ·
    2 years ago

    Have you ever seen someone use a tiling window manager instead of a desktop environment? Where it keeps all the currently running apps on screen in evenly sized tiles so you can see everything at once, nothing is in the “background?”

    Tmux is a bit like that, but only for the terminal. It allows you to open multiple terminals in one “screen” or terminal emulator window, and switch between them with keyboard shortcuts. So if you want to look at your source code, test run your source code, and watch htop to see how it performs, you can do that with Tmux. It’s a bit less cumbersome than opening three terminal windows.

    It also works over SSH, so you can SSH into a server or something, start tmux, then easily run several tools simultaneously.

    Tmux sessions are also persistent. Imagine if you were in the middle of working on something on your desktop at the office, then it’s time to go home. You can detach your session, SSH into the box from your laptop, reattach that session and keep working right where you left off.

    If you work in the terminal a lot, it’s a handy tool.

  • GustavoM@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    2 years ago

    It’s a “terminal inside your terminal” that you can cut into tiny little pieces where programs are run in said little pieces.

  • zikk_transport2@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    2 years ago

    In the company I work, we have to use jumpbox + “password” from proprietary code generator.

    Imagine going through this, then you suddenly need 2nd terminal. Inconvenience doing it again in another terminal?

    Well, there is a solution:

    1. tmux
    2. CTRL+B then ". And now you have 2 terminals.

    Also tmux is great for “quick solution” kind of things - to leave something running in the background. Talking about background - you can have many terminals open, from only 1 SSH session. :)