Searching the scrollback buffer in tmux

Suraj N. Kurapati

Before I switched to the tmux terminal multiplexer from the wmii window manager as my primary dwelling in the lands of Linux, I relied upon the URxvt terminal emulator‘s searchable-scrollback Perl extension, which let me search its scrollback buffer incrementally.

I ported that feature to tmux today, by feeding tmux’s scrollback buffer contents into Vim for incremental search or falling back to the less(1) pager for non-incremental search, like this:

# search buffer (using less if Vim is not available)
bind-key -n M-/ capture-pane -S -32767 -J \; new-window ' \
  tmux save-buffer - \; delete-buffer | {                 \
    vim -R -c "set nofen is hls ic" -;                    \
    test $? -eq 127 && less;                              \
  };                                                      \
' \; send-keys G ? # go to bottom and search upward

The neat thing about this snippet is that both vim and less(1) accept the same G (scroll to bottom of file) and ? (search backward) commands, so we jump directly into search mode no matter which of them is actually run.


Updates