Using tmux

Tags linux

Summary

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

Directions

  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.
  • Add a scrollbar to tmux window in Linux: echo "set -g terminal-overrides 'xterm*:smcup@:rmcup@'" >> ~/.tmux.conf
  • A good cheatsheet for tmux - https://tmuxcheatsheet.com/

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
Fri 1/26/24 12:46 PM