Tmux Tutorial: Following Guide
1. Introduction to Tmux
Tmux, short for Terminal Multiplexer, is a powerful tool that allows users to manage multiple terminal sessions within a single window. It enables the creation, access, and control of multiple terminals from a single interface, enhancing productivity for developers and system administrators.
---
2. A Brief History of Tmux
- Origins: Tmux was developed by Nicholas Marriott, with its first release in 2007. It was designed as an alternative to GNU Screen, another terminal multiplexer that had been in use for many years.
- Purpose: The primary motivation behind Tmux's development was to provide a modern and more feature-rich alternative to GNU Screen. It has since grown in popularity due to its ease of use, flexibility, and active development community.
- Evolution: Over the years, Tmux has seen numerous updates and improvements, with its community contributing to its growth and making it an essential tool in the Linux and Unix-like systems toolkit.
---
3. Why Use Tmux?
- Session Persistence: One of the most significant advantages of Tmux is its ability to maintain sessions even if the connection to the terminal is lost.
- Multiple Panes: Tmux allows users to split their terminal window into multiple panes, enabling multitasking without opening new terminal windows.
- Session Management: You can run multiple sessions in Tmux, switch between them, and even share sessions with other users.
- Customization: Tmux is highly customizable, allowing users to tailor it to their specific needs.
---
4. Installing Tmux
On Linux (Ubuntu/Debian):
sudo apt-get update
sudo apt-get install tmux
On macOS (using Homebrew):
brew install tmux
On Windows:
- Windows users can install Tmux via the Windows Subsystem for Linux (WSL) or through Cygwin.
---
5. Basic Tmux Commands
Once Tmux is installed, you can start using it with the following commands:
a. Starting a Tmux Session
tmux
This command starts a new Tmux session.
b. Creating a Named Session
tmux new -s session_name
This starts a new session with a specific name, making it easier to manage multiple sessions.
c. Detaching from a Session
Ctrl + b, d
This combination detaches you from the current session, leaving it running in the background.
d. Listing Sessions
tmux ls
This command lists all active Tmux sessions.
e. Attaching to a Session
tmux attach -t session_name
This command re-attaches you to a specific session.
f. Killing a Session
tmux kill-session -t session_name
This command ends a specific session.
---
6. Pane Management
One of Tmux's powerful features is its ability to split the terminal into multiple panes.
a. Splitting Horizontally
Ctrl + b, %
This splits the current pane horizontally.
b. Splitting Vertically
Ctrl + b, "
This splits the current pane vertically.
c. Switching Between Panes
Ctrl + b, arrow keys
Use the arrow keys to move between panes.
d. Resizing Panes
Ctrl + b, arrow keys
After pressing `Ctrl + b`, hold down the arrow keys to resize the current pane.
e. Closing a Pane
Ctrl + b, x
This closes the current pane.
---
7. Window Management
Tmux also supports the creation and management of multiple windows within a single session.
a. Creating a New Window
Ctrl + b, c
This creates a new window within the current session.
b. Navigating Between Windows
Ctrl + b, n # Next window
Ctrl + b, p # Previous window
c. Renaming a Window
Ctrl + b, ,
This command allows you to rename the current window.
d. Closing a Window
Ctrl + b, &
This closes the current window.
---
8. Customizing Tmux
Tmux is highly customizable through its configuration file (`~/.tmux.conf`).
a. Changing the Prefix Key
By default, Tmux uses `Ctrl + b` as its prefix key. You can change it to something more comfortable, like `Ctrl + a`:
set-option -g prefix C-a
unbind C-b
bind C-a send-prefix
b. Setting a Default Terminal
You can specify the default terminal Tmux should use:
set -g default-terminal "screen-256color"
c. Customizing the Status Bar
You can customize the status bar to display various information, like the date, time, or system load:
set -g status-right "#[fg=green]#H #[fg=yellow]%Y-%m-%d #[fg=cyan]%H:%M:%S"
---
9. Advanced Tmux Features
a. Scripting with Tmux
Tmux allows you to automate tasks using scripts. You can create a script to start a session, open multiple windows, and run commands in each window.
Example Script:
#!/bin/bash
tmux new-session -d -s my_session
tmux send-keys -t my_session:0 'htop' C-m
tmux new-window -t my_session:1
tmux send-keys -t my_session:1 'vim' C-m
tmux attach -t my_session
b. Copy Mode
Tmux has a built-in copy mode that allows you to scroll through the history and copy text:
Ctrl + b, [
Use the arrow keys to navigate, and press `Enter` to select text. To paste the copied text:
Ctrl + b, ]
---
10. Conclusion
Tmux is an incredibly versatile tool that can significantly enhance your productivity in the terminal. Whether you're managing multiple servers, writing code, or simply multitasking, Tmux provides a robust and flexible environment to get the job done efficiently. With this tutorial, you're now equipped with the knowledge to start using Tmux, customize it to your needs, and explore its advanced features.
---