LLI Wiki — My Digital Knowledge Base

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)

  1. Enter copy mode: Ctrl+b [
  2. Move the cursor to the start of the text to select, then press Ctrl+Space
  3. Move the cursor with the arrow keys to select the region
  4. At the end of the region, press Alt+w to copy the selection
  5. 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:

  1. Enter copy mode: Ctrl+b [
  2. Move the cursor to the start of the text, then press Space
  3. Move the cursor with the arrow keys
  4. Press Enter to copy the selection
  5. 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:

  1. Switch to the affected window, for example window 4: Ctrl+b 4
  2. Rename the window: Ctrl+b , then Ctrl+u, type the new name and confirm with Enter

Another option: Ctrl+b : then rename-window new_name.

Cheat sheet

A complete list of tmux commands is available in this public gist.

Mastodon LLIMastodon SocialFosstodon