Exploring the Benefits of Using `tmux`: A Quick Guide to Terminal Multiplexing

 

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

My first encounter with tmux left me unimpressed—I didn’t see the appeal. However, as I spent more time working with the terminal and faced various challenges, I revisited tmux and was amazed at how it revolutionized my workflow. This article aims to give a ten-minute introduction to the basics of tmux and its practical applications.

Understanding Terminal Sessions

Reflect on how you typically use the terminal in your daily work. You open an iTerm2 window, use SSH to connect to a remote machine, navigate to a specific directory, and start working. Once done, you close the iTerm2 window. This entire process is a terminal session. Its lifecycle is tied to the terminal’s lifecycle, meaning the session ends when you close the window. How can you decouple the session from the terminal, eliminating the need to repeat these steps each time? This is where tmux comes in handy.

Let’s explore how to use tmux to achieve this functionality.

ITerm2

In this demonstration, I create a tmux session using tmux new -s test, log into my development machine, detach the session, return to the iTerm2 terminal, and then use tmux attach-session to reconnect to the same development machine session exactly as I left it. This is the basic application of tmux: detaching and maintaining session states.

TL;DR

tmux enables us to:

  1. Access multiple sessions within a single window, useful for running several command-line programs simultaneously.
  2. Attach new windows to existing sessions.
  3. Allow multiple connected windows for each session, enabling real-time session sharing among multiple users.
  4. Support arbitrary vertical and horizontal window splitting.

Basic Usage of tmux

Installing tmux

On Mac, you can install tmux using brew:

1
brew install tmux

For other environments, refer to Installing tmux.

Starting and Exiting tmux

Once installed, type tmux in the terminal to start a tmux session. To exit the tmux session and return to the original terminal screen, simply enter exit.

ITerm2 2

Prefix Key

Unlike other software, all shortcuts in tmux are combined with the prefix key ⌃b (where is the control key on Mac). This reduces conflicts with other software. You can view all shortcuts by pressing ⌃b+?. tmux shortcuts generally fall into three categories: window, pane, and session management.

Session Management

Running the tmux command multiple times will open multiple tmux sessions. Within a tmux session, you can manage sessions using the prefix key ⌃b along with the following shortcuts:

  • ⌃b + $ — Rename the current session
  • ⌃b + s — Choose from a list of sessions
  • ⌃b + d — Detach the current session, returning to the terminal’s main screen.

In the terminal, sessions can be managed with the following commands:

  • tmux new -s foo — Create a new session named foo
  • tmux ls — List all tmux sessions
  • tmux a — Attach to the last session
  • tmux a -t foo — Attach to a session named foo; sessions are named numerically by default
  • tmux kill-session -t foo — Delete the session named foo
  • tmux kill-server — Delete all sessions

Using aliases can enhance your experience. For example, here are my custom configurations:

1
2
3
4
5
alias tnew='tmux new -s' # Create a new session
alias tls='tmux ls'
alias td='tmux detach' # Detach session, preserving the state before detachment
alias ta='tmux attach -t' # Attach to a session
alias tkss='tmux kill-session -t'

Pane Management

tmux can split windows into multiple panes, with each pane running a different command. These commands are executed within the tmux window.

  • ⌃b + % — Split the pane horizontally
  • ⌃b + " — Split the pane vertically
  • ⌃b + x — Close the current pane
  • ⌃b + { — Move the current pane to the left
  • ⌃b + } — Move the current pane to the right
  • ⌃b + ; — Switch to the last used pane
  • ⌃b + o — Switch to the next pane (you can also use arrow keys)
  • ⌃b + space — Cycle through pane layouts; tmux has five built-in layouts, switchable via ⌥1 to ⌥5
  • ⌃b + z — Maximize the current pane; repeat to restore to original size
  • ⌃b + q — Display pane numbers; press the corresponding number to switch to that pane

Window Management

tmux also supports the concept of windows. When panes become crowded, you can open a new window. Here are some commonly used shortcuts for managing windows:

  • ⌃b + c — Create a new window; this will switch to the new window without affecting the state of the existing window
  • ⌃b + p — Switch to the previous window
  • ⌃b + n — Switch to the next window
  • ⌃b + w — Choose from a list of windows; use ⌃p and ⌃n to navigate on macOS
  • ⌃b + & — Close the current window
  • ⌃b + , — Rename the window; supports Chinese characters for easy identification in the tmux status bar
  • ⌃b + 0 — Switch to window 0; use other numbers to switch to corresponding windows
  • ⌃b + f — Search and select windows by name; supports fuzzy matching

Conclusion

This article provides a basic summary of how to use tmux and its shortcuts. Many more advanced use cases exist, such as integrating with vim for more efficient coding. I hope this overview encourages you to try tmux and enhance your productivity with this powerful tool.

Licensed under CC BY-NC-SA 4.0
Last updated on Aug 16, 2024 17:54 CST
Built with Hugo
Theme Stack designed by Jimmy