tmux on WSL is a game-changer for Windows users who want a powerful terminal multiplexer without leaving their familiar environment.
Here’s how to get it running and a peek under the hood.
Setting Up tmux on WSL
First, ensure you have WSL installed and a Linux distribution (like Ubuntu) set up.
# Update your package lists
sudo apt update
# Install tmux
sudo apt install tmux -y
Once installed, simply type tmux in your WSL terminal to start a new session.
A Real-World Scenario: Remote Development
Imagine you’re SSH’d into a remote server for development. You need to:
- Run a build process.
- Tail a log file.
- Edit a configuration file.
Without tmux, you’d be juggling multiple SSH windows or tabs, which is cumbersome.
tmux in Action
Let’s simulate this. Open your WSL terminal:
tmux
You’re now in a tmux session. The status bar at the bottom shows 0:bash*.
1. Split the screen:
Press Ctrl+b (the default prefix key) then %. This splits the current pane vertically. You’ll see two panes.
2. Navigate between panes:
Press Ctrl+b then ← (left arrow) to move to the left pane.
3. Open a new window:
Press Ctrl+b then c. This creates a new window, indexed 1. You can see this in the status bar: 0:bash* 1:bash.
4. Back to window 0, split horizontally:
Press Ctrl+b then 0 to switch back to window 0. Then press Ctrl+b then ". This splits the current pane horizontally.
5. Run commands: In one pane, you might start a long-running process:
# In WSL terminal
while true; do echo "$(date) - Working..."; sleep 5; done
In another pane, you might tail a log file (even if it doesn’t exist yet, you can simulate it):
# In another WSL pane
touch myapp.log
echo "App started" >> myapp.log
tail -f myapp.log
In the third pane, you could edit a file:
# In the third WSL pane
nano myconfig.txt
You can switch between windows (Ctrl+b n for next, Ctrl+b p for previous) and panes (Ctrl+b followed by arrow keys) seamlessly.
The Mental Model: Sessions, Windows, and Panes
- Sessions: A tmux session is a collection of windows. You can detach from a session and reattach later, keeping your processes running. This is invaluable for remote work or unstable connections.
- Windows: Within a session, you have windows. Think of them as tabs in a browser. Each window can hold multiple panes.
- Panes: Windows are divided into panes. Each pane is an independent terminal emulator. You can split, resize, and navigate between them.
Detaching and Reattaching
To detach from your current session (leaving your processes running):
Press Ctrl+b then d.
You’ll see [detached (from session 0)].
To reattach to your session:
tmux attach
# Or if you have multiple sessions:
tmux attach -t 0
Configuration: .tmux.conf
You can customize tmux by creating a ~/.tmux.conf file. A common customization is to change the prefix key to something easier to press, like Ctrl+a.
# Change prefix to Ctrl+a
set -g prefix C-a
unbind C-b
bind C-a send-prefix
After saving this file, reload your tmux configuration:
Press Ctrl+a (your new prefix) then : and type source-file ~/.tmux.conf.
The One Thing Most People Don’t Know
The new-session and new-window commands can take an optional -d flag to create them detached. This is incredibly useful for scripting session setup. For example, tmux new-session -d -s 'myproject' 'nvim index.js' creates a detached session named myproject and immediately starts Neovim in the first pane. You can then tmux attach -t myproject to find your editor already open.
Next Steps
Understanding how to script tmux sessions for automated workflows or exploring plugins like tmux-resurrect will significantly enhance your productivity.