How to use copy mode in tmux?

Quick Answer

To enter copy mode in tmux, press Ctrl+b followed by [ (left square bracket). This lets you scroll, search, and copy text. Press q to exit copy mode.

Ctrl+b+[

Detailed Explanation

Copy mode in tmux allows you to navigate through terminal history, search for text, and copy content to the clipboard. It's essential for reviewing output that has scrolled off the screen and for copying text between panes or windows.

Basic copy mode operations:

  • Ctrl+b [ - Enter copy mode
  • q - Exit copy mode
  • Arrow keys - Navigate in copy mode
  • Page Up/Down - Scroll page up/down
  • Home/End - Go to start/end of line

Copying text:

  1. Press Ctrl+b [ to enter copy mode
  2. Navigate to the starting position of text to copy
  3. Press Space to start selection (or v in vi mode)
  4. Move cursor to select text
  5. Press Enter to copy selection to tmux buffer
  6. Press Ctrl+b ] to paste the copied text

Searching in copy mode:

  • / - Search forward
  • ? - Search backward
  • n - Next search match
  • N - Previous search match

Vim vs. Emacs Mode

tmux offers two key binding modes for copy mode: vi (Vim-like) and emacs. Set your preferred mode in ~/.tmux.conf:

# For vi mode
set-window-option -g mode-keys vi

# For emacs mode
set-window-option -g mode-keys emacs

Pro Tip: System Clipboard Integration

To copy text directly to your system clipboard, you'll need additional configuration:

# For macOS
set-option -g default-command "reattach-to-user-namespace -l $SHELL"
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy'

# For Linux with xclip
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'xclip -selection clipboard'