tmux: copy, paste and manage buffers
Copy and paste in tmux with emacs and vi key bindings, save buffers to a file, and fix a corrupted status bar.
With tmux, the default prefix is Ctrl+b. This page summarises how to copy and paste text using copy mode, then how to work with copy buffers. The shortcuts assume the default prefix.
Copy and paste in emacs mode (default)
- Enter copy mode:
Ctrl+b [ - Move the cursor to the start of the text to select, then press
Ctrl+Space - Move the cursor with the arrow keys to select the region
- At the end of the region, press
Alt+wto copy the selection - Paste with
Ctrl+b ]
In this mode, navigation uses emacs shortcuts: Ctrl+p, Ctrl+n, Ctrl+f, Ctrl+b.
Copy and paste in vi mode
To enable vi-style cursor movement in copy mode, add this line to ~/.tmux.conf:
set-window-option -g mode-keys vi
Then:
- Enter copy mode:
Ctrl+b [ - Move the cursor to the start of the text, then press
Space - Move the cursor with the arrow keys
- Press
Enterto copy the selection - Paste with
Ctrl+b ]
Working with copy buffers
Each copy is stored in a buffer. The following commands display them or save them to a file:
| Command | Effect |
|---|---|
Ctrl+b # |
Shows the list of buffers |
tmux list-buffers |
Lists the buffers |
tmux show-buffer |
Shows the current buffer |
tmux show-buffer -b n |
Shows the buffer with index n |
tmux save-buffer foo.txt |
Saves the current buffer to foo.txt |
tmux save-buffer -b n foo.txt |
Saves the buffer with index n to foo.txt |
Resetting a corrupted status bar
After a binary file has been displayed, a window's status bar can render incorrectly. The original note reports that renaming the window fixes it:
- Switch to the affected window, for example window 4:
Ctrl+b 4 - Rename the window:
Ctrl+b ,thenCtrl+u, type the new name and confirm withEnter
Another option: Ctrl+b : then rename-window new_name.
Cheat sheet
A complete list of tmux commands is available in this public gist.