tmux common commands and best practice

Last updated on September 24th, 2024 at 06:06 pm

Here are some simple and commonly used tmux commands that will help you navigate and manage your tmux sessions efficiently.

Basic tmux Commands

1. Start a new tmux session:

tmux

This will start a new tmux session and bring you into the tmux environment.

2. Create a new named tmux session:

tmux new -s session_name

Replace session_name with a name of your choice.

3. Attach to an existing session:

tmux attach-session -t session_name

If a tmux session is running, this will re-attach to it.

4. List all tmux sessions:

tmux list-sessions

This shows all active tmux sessions.

5. Detach from a session (without killing it):

Inside a tmux session, press:

Ctrl + b, then d

This detaches the session, allowing you to leave it running in the background.

6. Kill a session:

If you want to kill a session:

tmux kill-session -t session_name

Managing Windows and Panes in tmux

1. Create a new window:

Inside tmux, press:

Ctrl + b, then c

This creates a new window. Each window is like a separate terminal.

2. Switch between windows:

  • Switch to the next window:
    Ctrl + b, then n
    
  • Switch to the previous window:
    Ctrl + b, then p
    
  • Switch to a specific window by its number:
    Ctrl + b, then <window_number>
    

3. Split the current window into two panes (horizontal):

Ctrl + b, then "

This will split the window into two horizontal panes.

4. Split the current window into two panes (vertical):

Ctrl + b, then %

5. Switch between panes:

Ctrl + b, then arrow key

Use the arrow keys to move between the different panes.

6. Resize panes:

Hold Ctrl + b and use the arrow keys to resize the current pane:

Ctrl + b, then hold an arrow key

7. Close a pane:

To close a pane or window, you can simply exit the shell by typing exit or pressing Ctrl + d.


Other Useful Commands

1. Rename a window:

Ctrl + b, then ,

This allows you to rename the current window for easier identification.

2. Scroll within a pane:

Ctrl + b, then [

Use the arrow keys or PgUp/PgDn to scroll through the output in a pane.

3. Copy text from a pane:

  1. Enter copy mode:
    Ctrl + b, then [
    
  2. Move the cursor to select text (press Enter to copy).
  3. Paste the copied text by pressing:
    Ctrl + b, then ]
    

Session Management

1. Detach from tmux session:

Ctrl + b, then d

2. Rename a session:

Ctrl + b, then $

3. Switch between sessions:

  • List all sessions:
    tmux list-sessions
    
  • Switch to another session by its name:
    Ctrl + b, then s
    
    Then select the session you want to switch to.

Summary of Most Common Commands:

  • Create a new window: Ctrl + b, then c
  • Split panes (horizontal): Ctrl + b, then "
  • Split panes (vertical): Ctrl + b, then %
  • Move between panes: Ctrl + b, then arrow key
  • Detach from session: Ctrl + b, then d
  • List sessions: tmux list-sessions
  • Reattach to a session: tmux attach-session -t session_name
  • Kill a session: tmux kill-session -t session_name

By mastering these simple commands, you’ll be able to navigate and manage your tmux sessions efficiently.