2

I've run plenty of commands on my terminal, their results are displayed on it.

It's time I'm willing to search among what is written, without re-running the commands that produced these results.

Only by doing a grep on the current console content.
Does a way exist to do so ?

if, for exemple, this is displayed in my console:

Hello !
ERROR: bad argument: -2

Wait... Retry...

with other pages before (accessible by page up key), how do I grep it?

5
  • If you have a GUI terminal, it might have a search function. If it's the TTY and you weren't using something like screen or tmux, then I think you're out of luck.
    – muru
    Commented Oct 5, 2022 at 6:52
  • If the output is not saved anywhere (which it may be, in some GUI terminals, in some internal buffer), then you can't run grep on it. If the terminal application saves it, then you will have to investigate whether your terminal supports accessing that buffer somehow. You have not mentioned whether you are using a graphical terminal or not (although the console tag possibly implies that you don't).
    – Kusalananda
    Commented Oct 5, 2022 at 6:57
  • A possible but not great solution it's about redirecting all the output of your terminal (using exec before you run your commands) to some file and to the same terminal too (and when you need grep something you can do it in your file) Commented Oct 5, 2022 at 7:01
  • may be it is easier to start using a terminal multiplexer like GNU/Screen or tmux, both have search functions and a big buffer.
    – mestia
    Commented Oct 5, 2022 at 8:55
  • Having a terminal inside your editor is sometimes weird, but this is one of the use cases where M-x shell in Emacs really shines. There are some corner cases where it works poorly but in general, it works surprisingly well. Of course, you have to run the terminal inside Emacs in the first place to reap the benefits.
    – tripleee
    Commented Jan 3, 2023 at 16:04

2 Answers 2

4

Assuming you are on a Linux system, working on tty[A], then you should be able to use the backlog (/dev/vcs[A]) for that purpose.

Unfortunately, there are no newlines, so grepping into a single line…will not be very efficient. You'll need to format the output a bit first using fold

Assuming you get an 80 columns display, then:

fold -w 80 /dev/vcs[A] | grep TheStringYouWish

Should just do it.


Since fold splits lines longer than the specified width, if the searched pattern was originally part of such long lines, the above command will output only one part of the original line.
One could find handy to use the -A and -P options of grep to workaround this.

And of course you'll want to run that from another tty just in order not to possibly ruin your precious backlog of tty[A]… ;-P

0

If you have xclip installed then highlight/copy the chunk of text you want to search and then:

xclip -o | grep my_string

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .