tmux is an excellent terminal multiplexer that enables multiple windows within a single terminal session.
Configuration
The tmux configuration can be changed with a ~/.tmux.conf
file.
Command mode
Prefix
Once you’re in tmux, you can run a command by entering a prefix key followed by a command key.
By default, tmux uses Ctrl+b as the prefix for commands. I recommend changing this to Ctrl+a in your configuration file, which avoids conflicts with vim and is faster to type, with less strain.
Note that you must let go of the prefix before entering the command key.
There are many tmux commands available, but here are the only ones you need to get started:
Essentials
- d / :detach: Detach active session and exit back to normal terminal. When working inside a nested tmux session, hit the prefix key twice before detaching with d.
- c: Create a new window (works like a tab; appears in status bar).
- 1: Switch to window 1.
Panes (splits)
In situations where I’m working with multiple text documents, I find it quicker and easier to split windows inside of vim instead of with tmux. The advantage of doing this in vim is that it enables you to copy and paste text across documents in a more intuitive manner.
- %: Split vertically.
- ": Split horizontally.
- x: Kill active pane.
- o: Swap panes.
- q: Show pane numbers. When the numbers show up, you can type the key to goto that pane.
- z: Toggle pane zoom.
- +: Break pane into window (e.g. to select text by mouse to copy).
- -: Restore pane from window.
- {: Move the current pane left.
- }: Move the current pane right.
- ⍽+Space: Toggle between layouts.
Windows (tabs)
- c: Create window.
- &: Kill window.
- w: List windows.
- n: Next window.
- p: Previous window.
- f: Find window.
- ,: Name window.
Scrolling inside a pane
This is useful when needing to parse console output (e.g. interactive logs). Modified from StackExchange.
- [. The line numbers should appear in the top right corner.
- Now you can use your normal navigation keys to scroll around (eg. ↑ or PgDn).
- q quits scroll mode.
Copying and pasting text between panes
- Enter copy mode: prefix+[.
- Start selection: Space.
- Select the text to copy using arrow keys.
- Copy text: Enter.
- Switch to other pane.
- Paste text: prefix+].
Alternatively, here’s a quick method that works using the mouse.
- Zoom the pane using z.
- Copy the desired text using the mouse.
- Zoom back out with z.
Note that this approach generally only works well for short snippets.
Sessions
Note: If you run exit
inside tmux, that will kill the active session, rather than detaching.
List sessions.
tmux ls
Create a new session.
tmux new -s [name of session]
Attach the most recent session.
tmux a
Attach a previous session by name.
tmux a -t [name of session]
Attach a session by number.
tmux attach-session -t 3
Rename a session.
<PRE> :rename-session -t OLD_NAME NEW_NAME
Kill a session by number, without having to attach.
tmux kill-session -t 0
Kill all sessions.
tmux ls | grep : | cut -d. -f1 | awk '{print substr($1, 0, length($1)-1)}' | xargs kill
References
This guide was developed using these sources: