Using tmux

Tags linux how-to

Objective

Install and use tmux - a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. Furthermore, tmux may be "detached" from a screen and continue running in the background, allowing the user to logout, then later reattach the screen when logging back in.

It's an alternative to screen - in short, screen is more likely to be installed by default, but tmux has a couple more features and is more actively developed.

Environment

Procedure

  1. Install tmux
    • Red Hat: yum install tmux
    • Ubuntu: apt install tmux
    • macOS: brew install tmux or port install tmux
  2. Open a terminal.
  3. Enter: tmux


Use tmux commands by pressing the prefix, by default Ctrl-b, followed by a command key.

Sample Workflow to Run Long-Running Jobs

  1. Open a terminal and start tmux
  2. Start your job, e.g. python myscript.py
  3. Name your tmux session for ease of recall: Ctrl-b $
  4. Detach your tmux session so that it continues to run in the background: Ctrl-b d


At this point you are free to log out of the system, start a new tmux session that you can likewise name and detach, or otherwise continue working.

At any point, you can reattach to any previously detached tmux sessions with: tmux attach -t name

  • To list all tmux sessions enter: tmux ls
  • Output should look like:
      mypythonscript: 1 windows (created Thu Jun 13 14:01:40 2019) [114x30]
      testsession: 1 windows (created Thu Jun 13 14:01:24 2019) [114x30]
      

    For example, based on the previous sample output of tmux ls, tmux attach -t mypythonscript

     Caution
    Sessions persist through logouts, but they are lost if you shut down the computer!

    Tips and Tricks

    Here are some commonly-used tips and tricks:

    • Ctrl-b " (quotation mark) splits the active terminal window horizontally. Switch between the windows with Ctrl-b and then a directional arrow key.
    • Ctrl-b % (percent sign) splits the active terminal window vertically. Switch between the windows with Ctrl-b and then a directional arrow key.
    • Ctrl-b d (lowercase D) to detach the session (it keeps running in the background). You can logout, log back in, and tmux attach to re-attach a previously detached session.

    Additional notes

    tmux uses a prefix for all of its commands, by default it is Ctrl-b. This and many other behaviors of tmux can be changed via the configuration file ~/.tmux.conf. It's similar to vim in this sense with .vimrc. There's a plugin manager, with plugins for things like better mouse support for those who like using the mouse once-in-a-while. Here's a sample .tmux.conf for reference with pieces for a different prefix, scroll-wheel support, macOS clipboard integration, and the aforementioned better mouse support enabled.

    For best practices and more in-depth tips and tricks take a look at The Tao of tmux.

    Details

    Article ID: 1688
    Created
    Wed 5/27/20 9:58 AM
    Modified
    Tue 6/23/20 11:31 AM