Skip to main content
3 of 11
Add a complete statement
Hugh
  • 11
  • 2

I don't think I should re-paste the same content. But the stackeverflow bot seems to think so. In any case, it is convenient for peer reference. I'll rearrange it and put it here. Typing on a mobile phone makes it difficult to make major adjustments, sorry.

When searching the tmux-shell output twice, you will encounter a problem: the search input box does not support pasting. 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 they really understand what 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). 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 "wl-copy" ; tmux set -q @buffer_content "$(tmux show-buffer)"'
bind-key -T copy-mode-vi \\ command-prompt -I '#{@buffer_content}' -p '(search up)' 'send -X search-backward "%%%"'
bind-key : command-prompt -I '#{@buffer_content}' -p '>'

This version 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.

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. 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

Hugh
  • 11
  • 2