How to search through tmux buffer?

Quick Answer

To search text in tmux's buffer (scrollback history), first enter copy mode with Ctrl+b [. Then press / (in vi mode) or Ctrl+s (in emacs mode) to initiate search. Type your search pattern and press Enter. Navigate between matches with n/N (vi mode) or n/p (emacs mode) for next/previous matches. Press q to exit search and copy mode.

# Enter copy mode
Ctrl+b [

# Search (vi mode)
/search-term

# Search (emacs mode)
Ctrl+s search-term

Detailed Explanation

Searching through the tmux buffer is essential for finding specific information in command outputs, logs, or other text without scrolling manually. tmux offers powerful search capabilities in copy mode, with different key bindings depending on whether you're using vi or emacs key bindings.

Understanding tmux's Copy Mode

Copy mode allows you to navigate, search, and select text in the tmux buffer. Before you can search, you need to enter copy mode:

# Enter copy mode
Ctrl+b [

When in copy mode, you'll see a highlighted cursor position, and the status line will show "copy mode" or "copy mode (vi)" depending on your key binding settings.

Checking Your Key Binding Mode

The search commands differ depending on whether you're using vi or emacs key bindings. To check which mode you're in:

tmux show-options -g mode-keys

This will output either "mode-keys vi" or "mode-keys emacs".

Searching in Vi Mode

If you're using vi key bindings, searching works similar to vim:

# After entering copy mode (Ctrl+b [)

# Forward search
/pattern

# Backward search
?pattern

# Navigate to next match (same direction)
n

# Navigate to previous match (opposite direction)
N

For example, to search for "error" in the buffer, press Ctrl+b [ to enter copy mode, then type /error and press Enter.

Searching in Emacs Mode

If you're using emacs key bindings, searching works similar to emacs:

# After entering copy mode (Ctrl+b [)

# Forward search
Ctrl+s pattern

# Backward search
Ctrl+r pattern

# Navigate to next match
n

# Navigate to previous match
p

For example, to search for "error" in the buffer, press Ctrl+b [ to enter copy mode, then press Ctrl+s, type error and press Enter.

Case-Sensitive and Regex Searches

By default, tmux searches are case-sensitive. You can configure this behavior:

# Make searches case-insensitive
tmux set-option -g copy-mode-match-style none

# Make searches case-sensitive (default)
tmux set-option -g copy-mode-match-style fg=colour166,bg=colour234

tmux search also supports regular expressions. For example:

  • /error|warning - Search for "error" or "warning"
  • /^Starting - Search for lines beginning with "Starting"
  • /[0-9]{3} - Search for three consecutive digits

Copying Text After Searching

After finding text with search, you may want to copy it. In vi mode:

# Start selection at cursor
Space

# Navigate to end of text you want to copy
# (use h/j/k/l or arrow keys)

# Copy the selection
Enter

In emacs mode:

# Set mark at cursor
Ctrl+Space

# Navigate to end of text you want to copy
# (use arrow keys)

# Copy the selection
Alt+w

Exiting Search and Copy Mode

To exit search and copy mode, press q, Escape, or Ctrl+c.

Incremental Search

tmux version 3.2 and newer supports incremental search, which shows matches as you type:

# Add to ~/.tmux.conf
set-option -g copy-mode-Vi-search incremental-search on

# Or for emacs mode
set-option -g copy-mode-emacs-search incremental-search on

With incremental search enabled, matches will be highlighted in real-time as you type your search pattern, making it easier to find what you're looking for.