0

I need to simulate selecting (highlighting) text that is in the terminal buffer. It's long past stdout so all the grep/awk/sed/redirection stuff is not relevant AFAIK. Plus I don't want it highlighted during formatting of stdout. I need it to go to stdout unhighlighted, then highlight it. Simulating selection.

Here is the scenario...

A Bash script calls "ls -1" and outputs the following to stdout and then the terminal buffer?

$ ls -1
a.txt
b.txt
c.txt

Now I need a command I can put in a Bash script that will find and highlight "b.txt".

Like:

$ termsearch b.txt
a.txt
[b.txt]
c.txt

Where "termsearch" is a made up command I think I need and it converts b.txt to black text with white background (highlighted) and the rest of the text is white on black background. I think the problem is that you can't really edit the buffer and change text style like this.

I can use screen interactively to find "b.txt", but it doesn't highlight (the main goal) and I don't know if it's possible to script screen commands anyway. So I've given up on screen. The script command just dumps stdout to a file, but I don't want to search a file. I need to search the terminal buffer and highlight the results on screen without more stdout. What I want is already out, it just needs to be highlighted.

I'm thinking ncurses is the way to go, but I have never coded with it and it looks complicated. So before I dive in, I'm asking if anyone can think of another way to do this and validate that ncurses can even do what I want.

1
  • Don't give up on screen. That's your best bet. Or its more modern (but less ubiquitous) replacement, tmux.
    – Wildcard
    Nov 21, 2017 at 2:47

2 Answers 2

1

short: no, you can't

longer: The reason why you can't do that is that terminals as a rule don't allow programs to copy text from their screen. It's possible with a circuitous path to get a GUI application to select text from a terminal's screen, and if you then know what the text is, and where it is, you could in principle then issue commands in the terminal to tell it to repaint that section of the screen using whatever colors. But that's only hypothetical (unless someone's recently written a program to do this).

By the way, once the text is scrolled off the visible screen, terminals will not modify that, either.

2
  • So what code/library/driver is allowing me to use my mouse to select text from the term/buffer? I can even scroll way up to very old output and still select it with my mouse. Seems like if I can select it with my mouse, I should be able to select it with a script, but I'm guessing the answer is the selection with mouse is handled by the OS's GUI and not anything to do with the term itself. Perhaps ncurces will let me simulate a terminal output, but give more control over past output like searching and repainting.
    – user261502
    Nov 21, 2017 at 2:02
  • The terminal allows the X server to get the selection. But the terminal doesn't tell the X server where the text came from, nor does the text necessarily match what's shown on the screen. Lots of limitations :-) Nov 21, 2017 at 2:16
0

I would use vim.

For your particular case, I would do

vim .

Then vim will will present me a window with all the files in the directory. I can search with /, and open a file pressing Enter.

Just make sure search highlight is enabled with

:set hlsearch

For different commands, maybe this will be enough:

echo "one\ntwo\nthree" | vim -

You must log in to answer this question.

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