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?