Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I know many programs use color - (vim, ack, color-ls to name a few) but there are still many cases where you're stuck with black and white.

There are scripts that will color output based on regular expressions (i.e. "make anything that matches 'dog' red") but these must be invoked with pipes - for example: "> cat myfile | color dog red" and this is useless for anything interactive.

What I'm looking for is something that hooks the terminal emulator (or an terminal that actually does this) and colors anything that matches a given regex, independent of the shell or program i'm currently using. If this works with ncurses as well that would be awesome. I have too many monochrome programs and it would be great if I could specify a list of keywords to highlight/color when they appear or are typed.

I use Guake console which probably uses GNOME Terminal, I don't know if GNOME has this feature already.

share|improve this question
    
Are you looking for a GUI terminal which you would run under X, or a script/shell which you would invoke in any terminal, for example, over SSH? –  Josh Jul 24 '09 at 22:29

4 Answers 4

up vote 3 down vote accepted

Short version is: why, yes, you can pipe output from the shell, like any other program.

From there, you can use whatever you like, like, say

$ bash | sed "$(echo -e "s@dog@\e[31mdog\e[0m@g; s@more here@more here@g;")"

Thought I'd add my 'rainbow'. Enjoy:

for((b=0;$b<8;b++)); do echo -ne "\e[4${b}m4$b : "; \
  for((f=0;$f<8;f++)); do echo -ne "\e[3${f}m3${f}"; done; echo -e "\e[0m"; done
share|improve this answer

I do not have a solution but I have thought about how this kind of program should function. What you are asking is not straightforward.

For example, assume your regular expression is simple "flower". 1. If the terminal output is "f" "lower" we should perform the coloring. Or using an editor we first type "lower" and then go to the beginning of the word and type "f". 2. If the terminal output is "flower" we should cancel the already performed coloring. 3. If the output is slow, say five second delay between all the characters in "flower", we should not annoyingly stop the output, and should be able to immediately perform the coloring.

Either the functionality should be very tightly coupled with the terminal program (like you suggest), checking if any of the specified regular expressions matches the current line and then do the coloring. Or to make it more useful, we could use a program (little bit like /usr/bin/script) that acts like terminal device and keeps track of the screen output, for which it needs to know the terminal behavior very well; in this case we should also be able to redisplay the characters with different color (perform coloring and cancel already performed coloring).

You provide nice little programming idea...

share|improve this answer

I'm pretty sure KDE Konsole (at least the KDE3 version) could do this, however I don't see the support in KDE4.

You can do something with grep like this:

grep -C 99999 --color <whatever>

This should highlight matches but have so much context as to be the whole file.

share|improve this answer

There are probably better solutions than this, but you may be able to work something simple out with expect: http://expect.nist.gov/. There are expect libraries available for many other programming/scripting languages if you would rather avoid TCL.

In general, TCL is used for automating interactive programs, but it is possible to make them interactive as well.

I'd give you some code to start with but I haven't used it in quite a while myself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.