There is a general theme in GNU shell commands that if a program is able to deliver ANSI-colored output, it only does so in interactive shells, or if explicitly instructed. If you want to pipe output but have colors remain for later printing, you sometimes have to augment both the sending and the receiving command.
Of the many alternatives to
grep I use
Given the following
--flush --pager less -R -F
ack for something, it uses
less as a pager, and if the search results fit a screen, it just prints them, including colors, and otherwise it enters
less'es interactive mode.
ack parameters are explained:
ackto "flush output immediately. This is off by default unless ack is running interactively (when output goes to a pipe or file)." So with
--flush, output is always sent immediately.
ackto pipe its output through
less -R -F. This is equivalent to always running
ack ... | less -R -Fwhen typing
less parameters are explained:
--RAW-CONTROL-CHARS) prints ANSI color escape sequences. That means colored output and not a bunch of
--quit-if-one-screen) "causes less to automatically exit if the entire file can be displayed on the first screen."
The one drawback I have experienced so far is that if you do a very slow
ack with very few results that don't take up a lot of screen space, you won't get those results until
ack has finished or it eventually uses an entire screen, since then it knows to activate the pager rather than print directly.
You can circumvent this last quirk with
less -X (
--no-init) which "disables sending the termcap initialization and deinitialization strings to the terminal," but it means that
less will not wipe its output from the terminal when you hit q.
Decide which drawback you like less, or let me know of third options!