Ora

What Are the Basic Commands of Tmux?

Published in Tmux Commands 4 mins read

Tmux, often referred to as a terminal multiplexer, allows users to manage multiple terminal sessions, windows, and panes within a single terminal window. Its basic commands are designed to enhance productivity by enabling detachment from sessions, creating new windows, and splitting views into multiple panes.

Understanding the Tmux Prefix Key

All basic tmux commands begin with a "prefix key" combination. By default, this prefix is Ctrl-b (Control key and 'b' pressed simultaneously). Once the prefix key is pressed, you release it and then press the subsequent command key. This two-step process allows tmux to interpret your commands without interfering with the applications running within your terminal.

For example, to detach from a session, you would press Ctrl-b, release both keys, and then press d.

Essential Tmux Commands

Here's a breakdown of the fundamental commands you'll use most frequently when working with tmux:

Command Key Combination (after Ctrl-b) Action Description
d Detach Session Disconnects from the current tmux session, leaving it running in the background.
c Create New Window Opens a new, empty window within your current tmux session.
% Split Pane Vertically Divides the current window into two panes, arranged side-by-side (left and right).
" Split Pane Horizontally Divides the current window into two panes, arranged one above the other (top and bottom).
Arrow Keys (e.g., Up, Down, Left, Right) Move Between Panes Switches your active focus to an adjacent pane in the specified direction.
x Close Pane Kills the current active pane. You'll typically be prompted for confirmation before it closes.

Practical Usage of Basic Commands

Managing Sessions

One of tmux's most powerful features is the ability to detach from sessions. This allows you to start long-running processes, like a server or a software compilation, and then disconnect your terminal without interrupting the process. You can later re-attach to the session from the same or a different terminal.

  • Detach: After pressing Ctrl-b, press d. Your terminal will return to its original state, but your tmux session remains active in the background.
  • Listing Sessions: To see all your running sessions, exit tmux (if you're in one) and type tmux ls in your terminal.
  • Attaching to a Session: To re-enter a session, use tmux attach -t <session-name-or-number>. If you only have one session, tmux attach is sufficient.

Working with Windows

Within a tmux session, you can create multiple windows, each acting as a full-screen terminal. This is useful for organizing different tasks, such as having one window for code editing, another for running tests, and a third for log monitoring.

  • Create New Window: After pressing Ctrl-b, press c. A new, empty window will appear, and your previous window will remain accessible.
  • Navigate Windows:
    • Ctrl-b n: Go to the next window.
    • Ctrl-b p: Go to the previous window.
    • Ctrl-b <number>: Go to a specific window by its number (e.g., Ctrl-b 0 for the first window).

Splitting and Navigating Panes

Panes allow you to divide a single tmux window into multiple, independent terminal views. This is excellent for side-by-side comparisons, monitoring outputs while coding, or running multiple commands concurrently within one window.

  • Split Panes:
    • Ctrl-b %: Creates a new pane to the right of your current pane, splitting the window vertically.
    • Ctrl-b ": Creates a new pane below your current pane, splitting the window horizontally.
  • Move Between Panes: After splitting, you can easily move your active cursor to a different pane. Press Ctrl-b followed by an arrow key (Up, Down, Left, or Right) to jump to the pane in that direction.
  • Close Pane: To close the pane you are currently focused on, press Ctrl-b then x. Tmux will usually ask for confirmation before closing.

By mastering these fundamental tmux commands, you gain significant control over your terminal environment, improving your workflow and efficiency.