Use tmux for a more powerful terminal

Some Fedora users spend most or all their time at a command line terminal. The terminal gives you access to your whole system, as well as thousands of powerful utilities. However, it only shows you one command line session at a time by default. Even with a large terminal window, the entire window only shows one session. This wastes space, especially on large monitors and high resolution laptop screens. But what if you could break up that terminal into multiple sessions? This is precisely where tmux is handy — some say indispensable.

Install and start tmux

The tmux utility gets its name from being a terminal muxer, or multiplexer. In other words, it can break your single terminal session into multiple sessions. It manages both windows and panes:

  • window is a single view — that is, an assortment of things shown in your terminal.
  • pane is one part of that view, often a terminal session.

To get started, install the tmux utility on your system. You’ll need to have sudo setup for your user account (check out this article for instructions if needed).

sudo dnf -y install tmux

Run the utility to get started:

tmux

The status bar

At first, it might seem like nothing happens, other than a status bar that appears at the bottom of the terminal:

Start of tmux session

 

The bottom bar shows you:

  • [0] – You’re in the first session that was created by the tmux server. Numbering starts with 0. The server tracks all sessions whether they’re still alive or not.
  • 0:username@host:~ – Information about the first window of that session. Numbering starts with 0. The terminal in the active pane of the window is owned by username at hostname host. The current directory is (the home directory).
  • * – Shows that you’re currently in this window.
  • “hostname” – the hostname of the tmux server you’re using.
  • Also, the date and time on that particular host is shown.

The information bar will change as you add more windows and panes to the session.

Basics of tmux

Stretch your terminal window to make it much larger. Now let’s experiment with a few simple commands to create additional panes. All commands by default start with Ctrl+b.

  • Hit Ctrl+b, “ to split the current single pane horizontally. Now you have two command line panes in the window, one on top and one on bottom. Notice that the new bottom pane is your active pane.
  • Hit Ctrl+b, % to split the current pane vertically. Now you have three command line panes in the window. The new bottom right pane is your active pane.

tmux window with three panes

Notice the highlighted border around your current pane. To navigate around panes, do any of the following:

  • Hit Ctrl+b and then an arrow key.
  • Hit Ctrl+b, q. Numbers appear on the panes briefly. During this time, you can hit the number for the pane you want.

Now, try using the panes to run different commands. For instance, try this:

  • Use ls to show directory contents in the top pane.
  • Start vi in the bottom left pane to edit a text file.
  • Run top in the bottom right pane to monitor processes on your system.

The display will look something like this:

tmux session with three panes running different commands

So far, this example has only used one window with multiple panes. You can also run multiple windows in your session.

  • To create a new window, hit Ctrl+b, c. Notice that the status bar now shows two windows running. (Keen readers will see this in the screenshot above.)
  • To move to the previous window, hit Ctrl+b, p.
  • If you want to move to the next window, hit Ctrl+b, n.
  • To immediately move to a specific window (0-9), hit Ctrl+b followed by the window number.

If you’re wondering how to close a pane, simply quit that specific command line shell using exitlogout, or Ctrl+d. Once you close all panes in a window, that window disappears as well.

Detaching and attaching

One of the most powerful features of tmux is the ability to detach and reattach to a session. You can leave your windows and panes running when you detach. Moreover, you can even logout of the system entirely. Then later you can login to the same system, reattach to the tmux session, and see all your windows and panes where you left them. The commands you were running stay running while you’re detached.

To detach from a session, hit Ctrl+b, d. The session disappears and you’ll be back at the standard single shell. To reattach to the session, use this command:

tmux attach-session

This function is also a lifesaver when your network connection to a host is shaky. If your connection fails, all the processes in the session will stay running. Once your connection is back up, you can resume your work as if nothing happened.

And if that weren’t enough, on top of multiple windows and panes per session, you can also run multiple sessions. You can list these and then attach to the correct one by number or name:

tmux list-sessions

Further reading

This article only scratches the surface of tmux’s capabilities. You can manipulate your sessions in many other ways:

  • Swap one pane with another
  • Move a pane to another window (in the same or a different session!)
  • Set keybindings that perform your favorite commands automatically
  • Configure a ~/.tmux.conf file with your favorite settings by default so each new session looks the way you like

For a full explanation of all commands, check out these references:


Image courtesy of Reginar — originally posted to Unsplash.

Using Software

30 Comments

  1. wk3

    Cool information! I had never run across

    tmux

    before. I can think of several situations in the past where it would have saved a lot of time and frustration. Thanks!

  2. Steven

    I use tmux and powerline together as my terminal choice. It makes for an efficient terminal experience.

  3. wigust

    Also try out byobu package for better defaults.

  4. kapoath

    Can i run specific panes or even windows as root, while others not?

    Due to scripts running under root.

    • If you start a session under a certain user, then new windows and panes will, by default, be under that user, but yes you can run a process as root using sudo or su in a window or pane with the others staying unaffected.

    • GMaster

      Yes, that’s definitely possible.

    • Yes you can! They are all independent TTY sessions!

    • Daniel

      Kind of. You run tmux as root, and su to your other user in the panes you want. Or the other way around. Think about the security implications though,

    • MG

      Nothing stops you to switch to whatever user you need in any pane. Or use sudo. Whatever you can do in a shell.

    • twoCore

      You can start tmux as a “normal” user and run “su -” in a specific pane / window. So you can run scripts under root in the pane ( sorry for my english ). I use tmux for years to connect to multiple servers ( each pane has it own ssh connection ), which is very cool.

    • Yes you can. Different panels within tmux can be root and non-root (or even ssh’d into other machines as other users). Very powerful tool.

  5. chandan

    One of the features that I rely on is the fact that tmux is scriptable. Just typing a key combination will setup the dev & test environment within appropriate panes.

  6. RTG

    I tried it and like it. I’m always looking for new ways to increase efficiency while at the term. Currently use ansi-term from within emacs where I too can split windows, create multiple sessions and more. But tmux is something I can also use and recommend to others. Thanks!

  7. Tzvetan

    Hallo ,
    I don’t know why but the Ctrl+b is not working for me . Thanks

    Linux samurai 4.8.13-100.fc23.x86_64 #1 SMP Fri Dec 9 14:51:40 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

  8. MichaelC

    @kapoath
    Each pane is a new shell instance, so this an absolute yes.

    You can also create preconfigured sessions with panes in the size you need them.

  9. Thanks for article! Always want to know what is the difference between tmux and screen. Still doesn’t know, but at least I’ve tried tmux 😉

  10. Andrey Motoshkov

    Very useful (for myself at least) Ctrl-b,z: zoom-in/out the pane (make pane fullscreen and then back to previous size)

  11. Anon

    I think a Window manager like i3 does this better

  12. Which is better, tmux or screen ?

    • @André: It’s not really a matter of better/worse, although I think tmux may have the edge on features. But tmux is also actively maintained, and I believe screen, although stable, is not really updated regularly these days. On that basis I would recommend tmux.

    • mahetz

      I just use screen all the day, it does what i need, e.x. create a new named detached session with a script.
      But what I`m really missing is to capture a terminal started with a ssh login into a session, which a can detach.
      Can tmux do this, and most important, reliable?

  13. Nicolas

    Why not using Terminator instead? What are the pros about Tmux? Thanks!

    • John Holmes

      Since you can detach and reattach to a tmux session, it works incredibly well for remote terminal access. If your network connection drops or you need to reboot your local machine, you can pickup where you left off just by reconnecting and typing “tmux attach”. Terminator is amazing, but to me, this is tmux’s killer feature.

  14. Peeved Off

    my keybindings don’t work
    The only thing that works for me is:

    Ctl+b, ”

    And it doesnt behave as expected. It asks me for a index value for the window to switch to and since I have only one window it fails unless I add 0

    I tried adding a local conf file and rebinding to Ctl+C but same result.

    I even went through systematically using keys with the bind key and most didn’t work and none mapped to the documentation.
    I am running F25 with Wayland
    Arrrg.

    • @Peeved: I’m also running on F25 Workstation with Wayland so I can testify that tmux works fine. Try duplicating the issue on a brand new user account, and/or a fresh installation. It’s possible certain keyboard models or configurations could interfere with proper operation. Also, some non-standard desktop environments may have keybindings that interfere with tmux. Please use community help channels for more support, as this is not a support forum: https://fedoraproject.org/wiki/Communicating_and_getting_help

  15. Kyew

    For those who said i3 Window Manager or Terminator does a better job, you’re missing an important point. tmux works in within your terminal session, can continue running in the background, and was primarily made for remote sessions. You can start a tmux session, start a long running process in it, “disconnect” and then come back to it later. This works great if your connected via ssh to a server. You can run tmux, do your work, and if the connection drops you can re-establish your ssh session and then reconnect to the tmux session you dropped from. Whatever you were doing is still sitting there waiting for you to return.

    Another program called “screen” does pretty much the same thing.

  16. Jesse Vas

    Reminds me of Glenda. I like it!

  17. How can I get tmux to automatically start when I start my terminal.

    • @Kevin: You can usually set a command to run for each terminal in the settings for your terminal app. In the Fedora Workstation’s Terminal app, you can find this by editing the Profile (right click the Terminal or choose it from the app menu on the GNOME Shell menu bar). Edit the profile, choose Command, and use a custom command ‘tmux’.

    • This is what I use. Then I use tmux windows instead of tabs or different terminals.

      I have this in my bashrc

      Tmux Setup

      if [ -z “$TMUX” ]
      then
      tmux new-session -As default
      fi

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions