Dean
Dean

Reputation: 6950

Modify text already in the terminal

Is there a way to modify text already in the terminal but unlike a simple progress bar clearing with \r, by modifying text completely

E.g.

user@machine$ grep -R 'mystuff' .
result1
result2
result3
user@machine$ 

using arrows to e.g. to move a 'virtual' cursor (|)

user@machine$ grep -R 'mystuff' .
|result1
result2
result3
user@machine$ 

and most importantly, without clearing the terminal?

And no, it's not a duplicate of Modifying text in the terminal because it asks for a different kind of text manipulation while the description of the other question specifically asks for a progress bar example.

Upvotes: 3

Views: 1358

Answers (1)

chqrlie
chqrlie

Reputation: 144685

You can use escape sequences to perform various actions on the linux terminal. You can learn about this from the following references:

Controlling the terminal directly with escape sequences is very tricky because you must deal with all sorts of border cases and side effects, depending on the actual contents of the terminal and the type of text you display on it (ASCII, UTF-8 or other exotic variants...)

Your use case makes it even more difficult as you must first determine the contents of the terminal produced by unrelated commands such as grep, but also depending on the terminal specific width and height, the shell prompt, etc.

As the co-author and maintainer of qemacs, I can tell you how much of complete nightmare it has been to make the process buffer behave correctly, handling shell and programs output as well as user input in a transparent manner while at the same time making all this contents freely editable in the editor's buffers and windows. You can look at the source files tty.c and shell.c if you are not faint of heart.

Therefore I urge you to reconsider your problem from a different angle:

  • What are you trying to achieve?
  • Can you do it by filtering the output of commands?
  • Can you do it by creating shell aliasses or shell scripts that will post process the commands' output?
  • Can you use emacs, qemacs or some other shell enabled IDE to achieve the desired functionality?

Upvotes: 6

Related Questions