How can I search within the output buffer of a tmux shell?

I can't get this to work. How can I search the buffer of a tmux shell?


Solution 1:

copy mode search

To search in the tmux history buffer for the current window, press Ctrl-b [ to enter copy mode.

If you're using emacs key bindings (the default), press Ctrl-s then type the string to search for and press Enter. Press n to search for the same string again. Press Shift-n for reverse search. Press Escape twice to exit copy mode. You can use Ctrl-r to search in the reverse direction. Note that since tmux is in control of the keyboard in copy mode, Ctrl-s works regardless of the stty ixon setting (which I like to have as stty -ixon to enable forward searches in Bash).

If you're using vi key bindings (Ctrl-b:set-window-option -g mode-keys vi), press / then type the string to search for and press Enter. Press n to search for the same string again. Press Shift-n for reverse search as in emacs mode. Press q twice to exit copy mode. You can use ? to search in the reverse direction.

find-window

If you want to switch to a window based on something displayed in it (this also includes window names and titles but not history), (starting with more than one window open) press Ctrl-b f then type the string to search for and press Enter. You will be switched to a window containing that text if it's found. If more than one window matches, you'll see a list to select from.

Solution 2:

Enter copy mode and start searching in one go

bind-key / copy-mode \; send-key ?

allows you to do just:

 Ctrl + B /

and start typing the search term, which will search up (latest lines first).

Dump to a file and use vim

When things get more involved, I just want to use a proper editor: https://unix.stackexchange.com/questions/26548/write-all-tmux-scrollback-to-a-file

bind-key P 'capture-pane' \; capture-pane -S - \; save-buffer /tmp/tmux \; delete-buffer

Now P dumps the buffer to a file, and then I just:

vim /tmp/tmux

We can automate things even further by automatically opening vim as well as suggested by pkfm:

bind-key v 'capture-pane' \; \
  capture-pane -S - \; \
  save-buffer /tmp/tmux \; \
  delete-buffer \; \
  send-keys Escape 'ddivim /tmp/tmux' Enter

This supposes that your shell is in vi mode, so that:

  • Escape goes into normal mode
  • dd clears any existing command
  • i goes into insert mode
  • then we run vim /tmp/tmux

Tested in tmux 3.0.

Solution 3:

You can use vim to view/edit/search/save the screen log, fold the log at each bash prompt:

tmux capture-pane -pS -1000000 |
  vim +":setl fen fdm=expr fde=getline(v:lnum)=~'^\\\\S\\+\\\\$\\\\s'?'>1':1"  -  

Adjust the regex according to your prompt, use four backslash for each backslash in regex.

Or put the vim function in ~/.vimrc:

command!           MoshFoldTmuxLog :setl fen fdm=expr
  \   fde=getline(v:lnum)=~'^\\S\\+\\$\\s'?'>1':1 

And in ~/.bashrc add date to the prompt, if you have lots of logs to search through. e.g

PS1='\u@\h:\w:\D{%F-%T}$?:\$ ' # user-host-pwd-date-time-errno 
alias tmux-log='tmux capture-pane -pS -1000000 | vi +MoshFoldTmuxLog -'         

Solution 4:

Here is a solution I found.

You can modify the target path and filename as well:

# Save screen content to file
bind p command-prompt -p 'Save history to:' -I '~/tmux.history' 'capture-pane -S -32768 ; save-buffer %1 ; delete-buffer'

After reloading the tmux config file you can press prefix p in my case Ctrl+a p You can change bind p to your preferred key combination.

First mine was not working because I was overwriting bind p in another line so I just commented that out.