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

195

73

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

NES

Posted 2011-01-09T16:57:06.757

Reputation: 2 061

1What version of tmux (tmux -V)? I believe this was added in version 0.9. – Paused until further notice. – 2011-01-09T20:43:00.977

@Dennis it's 1.3-1 – NES – 2011-01-09T21:08:32.587

1Finding the version with tmux -V works only in later versions. In Ubuntu/Debian you can do: dpkg -l | grep tmux – Niels Bom – 2012-10-04T11:46:43.540

Answers

304

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.

Paused until further notice.

Posted 2011-01-09T16:57:06.757

Reputation: 86 075

3How can I set the binding style? (emacs vs vi) – Daniel Que – 2014-08-12T22:39:13.220

2@DanielQue: Take a look at the tmux man page and search for "mode-keys" and "status-keys". Those are sub-commands that allow you to set the binding style. Alternately, it might be simpler to set an environment variable (EDITOR or VISUAL) to the style you want before starting tmux. – Paused until further notice. – 2014-08-12T22:55:40.253

6Thanks, I got it to work with set-window-option -g mode-keys vi in my .tmux.conf. But I was curious about the environment variable alternative and couldn't get it to work. Is it a shell environment variable, or a tmux environment variable that has to be set in the conf file? – Daniel Que – 2014-08-12T23:38:48.043

2@DanielQue: A shell environment variable. It will need to be exported or placed in tmux's envrionment like this: VISUAL=vi tmux – Paused until further notice. – 2014-08-13T04:54:21.043

1

Also note that there is no regex search yet, here is an open issue on it http://sourceforge.net/p/tmux/tickets/9/

– Elijah Lynn – 2014-10-28T20:58:01.850

Is there a way to search all windows within all sessions? Thanks! – cat pants – 2018-01-25T04:29:29.467

@catpants: I don't know about across sessions, but I cover searching across windows in my answer. – Paused until further notice. – 2018-01-25T18:32:32.760

Warning - this does not handle newlines. e.g if you search for a string, but that string occurs split across multiple lines, you will miss the split results! – Adverbly – 2020-01-15T21:01:35.353

10

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.

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

Tested in tmux 2.6.

Ciro Santilli 新疆改造中心法轮功六四事件

Posted 2011-01-09T16:57:06.757

Reputation: 5 621

How do I make this prompt for the filename (so I don't have to hardcode /tmp/tmux)? – Peeyush Kushwaha – 2018-10-19T08:06:07.343

@PeeyushKushwaha sorry but my tmux fu is not that good, I'd have to google, let me know if you find it out. – Ciro Santilli 新疆改造中心法轮功六四事件 – 2018-10-19T08:08:06.537

1

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

Posted 2011-01-09T16:57:06.757

Reputation: 227

0

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

Posted 2011-01-09T16:57:06.757

Reputation: 101