349

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

NES
  • 3,601

6 Answers6

505

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.

40

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.

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 -'         
mosh
  • 317
2

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.

jturi
  • 141
1

Search for Command Prompt

This solution originate from [iron-code][1]. To search for the last prompt add the following code to your tmux.conf file.

# This code should be added to "tmux.conf" or any file which get loaded during tmux startup
#
# Search for command prompt from top of the terminal session (forward) and backwards

set prompt search pattern to identify command line input

e.g. SHELL_PROMPT_REGEXP="$ "

SHELL_PROMPT_REGEXP="➜ "

bind-key -N "Search backward for command prompt" "M-]" { copy-mode send-keys -XF search-backward $SHELL_PROMPT_REGEXP send-keys "n" }

bind-key -N "Search from top for command prompt" "M-[" { copy-mode send-keys -XF search-forward $SHELL_PROMPT_REGEXP }

Reload your config. Typing LEADER M+] let you jump to the last command prompt in copy mode. Whereby LEADER is your prefix key, e.g., C+b.

If you are using copy-mode-vi, typing the key combination n will show you the second last command prompt, while N key let you search forward.

In case you prefer to search from top of your terminal log, LEADER M+[ shows up the first prompt, and every typed n jumps to the next prompt.

This solution does not work well if the command outputs characters are similar to your prompt.
[1]: https://github.com/salcode/ironcode-tmux/issues/21

abu_bua
  • 558
  • 9
  • 9
-1

When searching the tmux-shell output twice, you will encounter a problem: the search input box seems not support pasting originally. I think this is why people want a real editor or shell to complete complex searches. I replied to another akin question. I'm not sure if people are willing to consider going in the direction I'm talking about, so I put the link (Original post title: TMUX: can you paste the most recent buffer into the search field) here for reference by those who need it :)

The content of the original post is roughly as follows:

In my tmux version 3.2a, there are indeed preset search keys (# and *) under copy-mode-vi.

bind-key -T copy-mode-vi \# send-key -FX search-backward "#{copy_cursor_word}"
bind-key -T copy-mode-vi * send-key -FX search-forward "#{copy_cursor_word}"

They make use of #{copy_cursor_word} directly as the search target, but what if the text at the cursor is a continuous entire paragraph of text (such as the value of a cmake variable)? If you do this kind of search often, you'll understand that it's not that simple. We need a more precise search.

Version 1

bind-key -T copy-mode-vi \\ command-prompt -I "#{copy_cursor_word}" -p "(search up)" "send -X search-backward \"%%%\""

(You can change the hotkey '\' to the appropriate key value in your tmux configuration environment. )

Through this definition, you can edit the content captured by tmux and submit the search keywords you care about.

The poster did raise a good question. In the pure tty environment he specifically refers to, tmux "is" the system itself and graphics systems may not exist yet :)

My similar working scenario is: The shell searches for keywords in files under a specific path, and then tmux traverses the values of key variables in the shell search results. The default copy-mode-vi search of tmux requires manually entering keywords — there is no way to paste easily under a pure tty environment IMHO (let’s say a linux console mode — no mouse, no GUI and please don't say prefix ], we are talking about the copy-mode-vi mode's search input window, not the paste operation after exiting this mode). Therefore, An alternative method adopted — selecting keywords in the shell search results (version 1) — this is currently not ideal and is being improved.

Improved version after introducing shell script:

Version 2

bind-key -T copy-mode-vi \\ run-shell 'tmux command-prompt -I "$(tmux show-buffer)" -p "(search up)" "send -X search-backward \"%%%\""'

But, a question arises here: If the copied content is a tmux script, will it seem to be executed? For example, in man tmux

#{?pane_in_mode,#[fg=white#,bg=red],#[fg=red#,bg=white]}#W .

When you yank this text under copy-mode-vi, the search box will not display anything. If it's plain-text, there's no problem.

So, version 1 isn't as bad as it looks, right?

Version 3

bind-key -T copy-mode-vi y run 'tmux send-keys -X copy-pipe ; \
tmux set -q @buffer_content "#{l:$(tmux show-buffer)}"'
bind-key : command-prompt -I '#{@buffer_content}' -p '>' \; \
run 'tmux set -u "@buffer_content"'
# Don't duplicate the following \\ with the version 4
bind-key -T copy-mode-vi \\ command-prompt -I '#{@buffer_content}' -p '(search up)' 'send -X search-backward "%%%"'

The version 3 avoids the problems of the first two versions (but still does not implement the paste action itself). I apply it to two situations: whether copying screen content into the "copy-mode-vi" mode search box or the line editor of the "command-prompt" mode interactive window, it is based on the same logic: define an additional @buffer_content to store the latest buffer variable, it will be automatically pasted into the currently executing line editor in its original form. You can edit and submit the content normally for further processing.

Here is a related link Run arbitrary command in tmux copy mode. The method it provides is the same as the method I use. It has the advantage of automatic copying selection since it must work under the copy-mode (via send-keys), which implementation still requires caching to a custom tmux variable on my machine and should use the "-I" interface. My relevant version shows below:

Version 4

bind-key -T copy-mode-vi \\ send-keys -X copy-selection \; \
run 'tmux set -gq @buffer_command "#{l:$(tmux show-buffer)}"' \; \
command-prompt -I '#{@buffer_command}' -p 'search up>' 'send -X search-backward "%%%"' \; \
run 'tmux set -u "@buffer_command"'

bind-key -T copy-mode-vi : send-keys -X copy-selection ;
run 'tmux set -gq @buffer_command "#{l:$(tmux show-buffer)}"' ;
command-prompt -p '>' -I '#{@buffer_command}' ;
run 'tmux set -u "@buffer_command"'

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

The above code can work normally in the KISS Linux Console environment (Basically, in the minimal environment of Linux , except for tmux itself). Different results may be obtained in different systems. If you have a different implementation or a better solution, please reply or open another topic for discussion. Thanks

Update: Tmux 3.4 supports C-y on status-keys equals vi. The above implementation omits the paste operation in the workflow, which has its own advantages for searching/debugging under shell environment.

Hugh
  • 11
  • 2