Slightly improved colored cal output

Alec Jacobson

July 29, 2013

weblog/

I found a nice alias to color the output of the cal function. But on the 20th of July this produced:

     July <span style="color:red;font-weight:bold;">20</span>13
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 <span style="color:red;font-weight:bold;">20</span>
21 22 23 24 25 26 27
28 29 30 31

I refined the grep search a bit with this

alias cal='cal | grep --color=auto -E "( |^)$(date +%e)([^0-9]|$)|$"'

which produces the correct result:

     July 2013
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 <span style="color:red;font-weight:bold;">20</span>
21 22 23 24 25 26 27
28 29 30 31

Update: The alias above ruins the ability to pass arguments to cal. Here's a slightly improved version:

cal() { /usr/bin/cal "$@" | grep --color=auto -E "( |^)$(date +%e)([^0-9]|$)|$";}

Unfortunately it'll still color today's day of the week even if the month or year passed in the cal arguments are not the current month or year.