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
-
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. -
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.
-
Window Management: Within each session,
tmux
allows for the creation of multiple windows, enabling users to organize their work more efficiently. -
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.
-
Scriptability:
tmux
supports scripting, which allows automating and customizing the work environment, such as setting up a predefined layout of windows and panes. -
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
-
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.
-
Development: Developers use
tmux
for an integrated development environment in the terminal, managing editors, servers, and logs in one place. -
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
ortmux new -s session_name
- Attach to session:
tmux attach -t session_name
- Detach from session:
Ctrl+b
thend
- List sessions:
tmux ls
- Kill session:
tmux kill-session -t session_name
Managing Windows
- Create window:
Ctrl+b
thenc
- Switch to window:
Ctrl+b
then0
...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
thenCtrl
+ arrow keys - Close current pane:
Ctrl+b
thenx
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.