Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Colorize command output



zzapper wrote:
> On Mon, 21 Feb 2005 17:16:43 +0100,  wrote:
> 
> >
> >To make conflicts stand out, I'd like to colorize the output of the svn 
> >command so that lines starting with C are displayed in red. My first 
> >shot at this was
> >
> >% svn status | sed -e 's/^C/\e[31mC\e[0m/g'

Escapes like \e are not expanded by sed. You can used the shell's $'...'
form of quoting to expand them, however.

    svn status | sed -e $'s/^C/\e[31m&\e[0m/g'

Note also, that you can use & in the replacement part of sed's s
command. That substitutes whatever was matched.

> I guess you could cheat by using grep as a colorizer
> 
> echo "fred" | grep --color '.' 

That's not quite the same. It highlights matching parts of a line so
lines not beginning with a C will be lost. I've attached below my old
hgrep script which highlights matches but outputs all lines. I find it
more useful than grep --color. It's meant for general cases, though, and
wouldn't be much use for colouring svn output.

Oliver

# hgrep - highlight grep

if (( ! $# )); then
  echo "Usage: $0:t [-e pattern...] [file...]" >&2
  return 1
fi

local -a regex
local htext=`echotc so` ntext=`echotc se`

while [[ "$1" = -e ]]; do
  regex=( $regex "$2" )
  shift 2
done

if (( ! $#regex )); then
  regex=( "$1" )
  shift
fi

regex=( "-e
s/${^regex[@]}/$htext&$ntext/g" )
sed ${(Ff)regex[@]} "$@"



Messages sorted by: Reverse Date, Date, Thread, Author