Skip to main content

tmux (Terminal Multiplexer)

tmux is a terminal multiplexer, a command-line tool used to manage multiple terminal sessions within a single window. It's popular in Unix-like operating systems, such as Linux and macOS.

Purpose

  1. Multiple Sessions: tmux allows users to create, access, and control several terminal sessions from a single screen. This feature is particularly useful for working with multiple command-line applications simultaneously.

  2. Session Detachment and Reattachment: Users can detach from a session and leave it running in the background, then later reattach to it. This is extremely useful for long-running processes, especially when connecting via SSH (Secure Shell), as the process continues even if the connection is lost.

  3. Window Management: Within each session, tmux allows for the creation of multiple windows, enabling users to organize their work more efficiently.

  4. Pane Splitting: Users can split windows into panes, allowing for side-by-side command-line work. This feature is helpful for comparing outputs, monitoring multiple streams of data, or multitasking.

  5. Scriptability: tmux supports scripting, which allows automating and customizing the work environment, such as setting up a predefined layout of windows and panes.

  6. Persistence: Sessions in tmux are persistent, meaning they survive user logouts or network disconnections, which is particularly useful for remote work.

How it Works

  • Starting tmux: A user enters tmux in the command line to start a new session.
  • Navigating Sessions: Within tmux, users can create new sessions, switch between them, or close them as needed.
  • Windows and Panes: Users can open multiple windows within a session and split these windows into panes.
  • Detach/Attach: Users can detach from a session and later reattach to it. This maintains the state of the session as it was left.

Applications

  1. Remote Work: It's widely used by system administrators and developers who connect to remote servers via SSH, as it ensures continuity of work irrespective of the network connection.

  2. Development: Developers use tmux for an integrated development environment in the terminal, managing editors, servers, and logs in one place.

  3. Command-Line Tasks: Ideal for managing multiple command-line tasks simultaneously, monitoring, and comparing outputs in real-time.

Command Examples

Starting and Managing Sessions

  • Start new session: tmux or tmux new -s session_name
  • Attach to session: tmux attach -t session_name
  • Detach from session: Ctrl+b then d
  • List sessions: tmux ls
  • Kill session: tmux kill-session -t session_name

Managing Windows

  • Create window: Ctrl+b then c
  • Switch to window: Ctrl+b then 0...9
  • Rename current window: Ctrl+b then ,
  • Close current window: Ctrl+b then &

Managing Panes

  • Split pane horizontally: Ctrl+b then "
  • Split pane vertically: Ctrl+b then %
  • Navigate between panes: Ctrl+b then arrow keys
  • Resize panes: Ctrl+b then Ctrl + arrow keys
  • Close current pane: Ctrl+b then x

Miscellaneous

  • Enter scroll mode: Ctrl+b then [, navigate using arrow keys, q to exit
  • Capture pane content to file: Ctrl+b then :capture-pane -S -100 then :save-buffer filename.txt
  • Reload config file: tmux source-file ~/.tmux.conf
  • Show help: Ctrl+b then ?

Conclusion

tmux is a powerful tool for enhancing productivity at the command line. Its ability to manage multiple terminal sessions efficiently, maintain persistence, and offer flexible window and pane management makes it a go-to tool for developers, system administrators, and power users working in Unix-like environments.