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
This is by design: programs that produce colored output typically do so only when their output goes to a terminal, not when it's sent to a pipe or to a regular file. The reason is that data sent on a terminal is presumably read by a human, whereas data piped to a program or written to a file is likely to be parsed by some program, so it shouldn't contain extraneous content like color-changing escape sequences.
GNU
ls
displays colored output on a terminal when you pass the option--color
(or--color=auto
). To force colored output regardless of the file type of the standard output, pass--color=always
or--color=yes
(they're synonyms). This convention has been followed by other commands, like GNU grep, FreeBSD grep,git diff
, etc.With the FreeBSD version of
ls
(also found on OSX, and available as thecolorls
port on OpenBSD and NetBSD), pass the option-G
to display colors when the output is a terminal. Set the environmentCLICOLOR_FORCE
to display colors regardless of the output file type.