tmux - Linux


Overview

tmux is a terminal multiplexer, allowing users to access multiple terminal sessions inside one single window. It is useful for running more than one command-line program at the same time. Additionally, it can detach and reattach sessions, maintaining state across connections, which is particularly useful in remote sessions.

Syntax

The basic syntax for tmux is:

tmux [options] [command]

The command is optional and if omitted, tmux will start a new session.

Options/Flags

  • -2: Forces tmux to assume the terminal supports 256 colors.
  • -u: Forces tmux to assume UTF-8 character set.
  • -L socket-name: Use this flag to specify a different socket name, useful for running separate tmux instances.
  • -t target-session: Used with various commands (like attach, kill-session) to specify the session.
  • new-session, new or neww: Creates a new session.
  • attach-session or attach: Attaches to an existing session.
  • detach-client or detach: Detaches the currently attached client.
  • list-sessions or ls: Lists current sessions.
  • kill-session: Kills the specified session.

Examples

  1. Starting a New Session:

    tmux
    
  2. Creating a New Named Session:

    tmux new -s mysession
    
  3. Attaching to a Named Session:

    tmux attach -t mysession
    
  4. Detaching from the Current Session:

    Using the key combination: Ctrl+b d

  5. Splitting Window Vertically:

    Inside tmux, press Ctrl+b %

Common Issues

  • Lost Connection: If users detach a session and forget the session name, they can find it again with:

    tmux list-sessions
    
  • Default key bindings collide with user’s core habits. Customizing key bindings in .tmux.conf file can mitigate this issue.

Integration

tmux can be integrated with various tools for advanced scripting and automation:

ssh user@host -t 'tmux attach || tmux new'

This command connects to a remote server and attaches to a tmux session or creates a new one if none exist.

  • screen: Similar functionality as tmux but with less customization options available.
  • byobu: Enhances tmux and screen, adding additional visual information and management features.

For further reading and more detailed information, you can refer to the official tmux documentation or man tmux on your command line.