How to use copy mode to scroll 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

Vim vs. Emacs Mode

tmux offers two key binding modes for copy mode: vi (Vim-like) and emacs. Each provides different navigation styles and key combinations based on their respective editors. 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
Vi Mode Key Bindings:
  • h, j, k, l - Move cursor left, down, up, right
  • v - Start selection (visual mode)
  • y - Yank (copy) selected text
  • 0 - Start of line, $ - End of line
  • / - Search forward, ? - Search backward
  • n - Next search match, N - Previous match
Emacs Mode Key Bindings:
  • Ctrl+b - Move cursor left
  • Ctrl+f - Move cursor right
  • Ctrl+p - Move cursor up
  • Ctrl+n - Move cursor down
  • Ctrl+Space - Set mark (start selection)
  • Alt+w - Copy selected text
  • Ctrl+a - Start of line, Ctrl+e - End of line
  • Ctrl+s - Search forward, Ctrl+r - Search backward

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'