Use:
git diff --color=always | less -r
--color=always
is there to tell git
to output color codes even if the output is a pipe (not a tty). And -r
is there to tell less
to interpret those color codes and other escape sequences. Use -R
for ANSI color codes only.
UPDATE: I've added a new (different) script... Ignacio Vazquez-Abrams
had a point: The question really asks for executable scripts are green, et cetera
.. okay... you'll find such a (prototype) script at the end of this answer.
This first (original) section is about grc
and grcat
.
This should work; grc
... (as enzotib has pointed out.. The package name is grc
... The sub-utility used in the example, is grcat
generic colouriser for everything
generic colouriser, can be used to colourise logfiles,
output of commands, arbitrary text....
configured via regexp's.
The following example prints
./
in magenta
bin/cpp/
in cyan
bigint
in bold white
I haven't fully sorted out how it handles it config file yet, but this looks like it will do what you want (once you tame it).. eg. for a file with no sub-dir, and the color sequence seems to not be in the same sequence as the expressions.
I assume it is possible (but I'm a bit busy at the moment)...
echo "# my config file
regexp=(\./)(.*/)([^/]+)
colours=bold white,magenta,cyan
">$HOME/.grc/findhi
find . -maxdepth 3 -name '*' | grcat findhi
Here is the new Ignacio inspired script :)
This works if you use a single path as the first arg to find
.
There are UNTESTED issues in this script. It is only concept.
One issue is: Symbolic Links... murky waters...
As-is, it prints an ERROR
when it encounters an unknown type (eg. a symbolic link), and then continues processing past that.
Thanks to enzotib
for the tput
examples.
dircol=$(tput bold ;tput setaf 4)
coloff=$(tput sgr0)
root="$HOME" # define path here, not in 'find` arg
root="${root:-.}" # default to '.'
root="${root%/}/" # add trailing '/'
#
find "$root" -maxdepth 1 -name '*' -printf "%y %P\n" |
while read -r line ;do
case $line in
d ) printf "%s\n" "$dircol$root$coloff";;
d\ *) printf "%s\n" "$dircol$root${line:2}$coloff";;
f\ *) l="$root${line:2}"
d="${l%/*}/"
f="${l##*/}"
cd -P "$d"
printf "%s" "$dircol$d$coloff"
ls --color=always -R1 "$f"
cd - >/dev/null
;;
*) printf "ERROR - type not yet catered for\n";;
esac
done
Best Answer
Some of the reasons OP has stated the options are unsuitable have no basis in reality. Here, I show what kind of effects using OP's strategy 4 has:
On most distributions,
grep
is installed in/bin
(typical) or/usr/bin
(OpenSUSE, maybe others), and defaultPATH
contains/usr/local/bin
before/bin
or/usr/bin
. This means that if you create/usr/local/bin/grep
withwhere
/bin/sh
is a POSIX-compatible shell provided by your distribution, usually bash or dash. Ifgrep
is in/usr/bin
, then make thatThe overhead of this script is minimal. The
exec
statement means that the script interpreter is replaced by thegrep
binary; this means that the shell does not remain in memory whilegrep
is being executed. Thus, the only overhead is one extra execution of the script interpreter, i.e. a small latency in wall clock time. The latency is roughly constant (varies only depending on whethergrep
andsh
are already in page cache or not, and on how much I/O bandwidth is available), and does not depend on how longgrep
executes or how much data it processes.So, how long is that latency, i.e. the overhead added by the wrapper script?
To find out, create the above script, and run
On my machine, the former takes 0.005s real time (across a large number of runs), whereas the latter takes 0.006s real time. Thus, the overhead of using the wrapper on my machine is 0.001s (or less) per invocation.
This is insignificant.
I also fail to see anything "dirty" about this, because many common applications and utilities use the same approach. To see the list of such on your machine in
/bin
and/usr/bin
, just runOn my machine, the above output includes
egrep
,fgrep
,zgrep
,which
,7z
,chromium-browser
,ldd
, andxfig
, which I use quite often. Unless you consider your entire distribution "dirty" for relying on wrapper scripts, you have no reason to consider such wrapper scripts "dirty".As to problems such a wrapper script may cause:
If only human users (as opposed to scripts) are using the version of grep that defaults to color support if output is to a terminal, then the wrapper script can be named
colorgrep
orcgrep
or whatever the OP sees fit.This avoids all possible compatibility issues, because the behaviour of
grep
does not change at all.Enabling
grep
options with a wrapper script, but in a way that avoids any new problems:We can easily rewrite the wrapper script to support a custom
GREP_OPTS
even ifGREP_OPTIONS
were not supported (as it is already deprecated). This way users can simply addexport "GREP_OPTIONS=--color=auto"
or similar to their profile./usr/local/bin/grep
is thenNote that there are no quotes around
$GREP_OPTIONS
, so that users can specify more than one option.On my system, executing
time /usr/local/bin/grep --version
withGREP_OPTIONS
empty, or withGREP_OPTIONS=--color=auto
, is just as fast as the previous version of the wrapper script; i.e., typically takes one millisecond longer to execute than plaingrep
.This last version is the one I'd personally recommend for use.
In summary, OP's strategy 4:
is aready recommended by
grep
developersis trivial to implement (two lines)
has insignificant overhead (one millisecond extra latency per invocation on this particular laptop; easily verified on each machine)
can be implemented as a wrapper script that adds
GREP_OPTS
support (to replace deprecated/unsupportedGREP_OPTIONS
)can be implemented (as
colorgrep
/cgrep
) that does not affect scripts or existing users at allBecause it is a technique that is widely used in Linux distributions already, it is a common technique and not "dirty".
If implemented as a separate wrapper (
colorgrep
/cgrep
), it cannot create new problems since it does not affectgrep
behaviour at all. If implemented as a wrapper script that addsGREP_OPTS
support, usingGREP_OPTS=--color=auto
has exactly the same risks (wrt. problems with existing scripts) that upstream adding default--color=auto
would. Thus, the comment that this "creates more problems than it solves" is completely incorrect: no additional problems are created.