I like the colors I have in vim. Can I get my output similarly colorized when I do a cat?

I like that I can use the colorize tool as in Colorized `cat` for source and script files?. I would like that to be my standard for cat, e.g. I create an alias:

alias cat="source-highlight --out-format=esc -o STDOUT -i"

However if the file type is unknown, say .gitignore then this will return:

$ cat .gitignore_global
source-highlight: could not find a language definition for input file .gitignore

How could I have the command do a source-highlight cat version if it is a recognized file type and otherwise just do a standard black and white cat of the file?

One option is to have the alias be ccat but I'd prefer to have it replace cat itself if possible.

It would also be nice to be able to use wildcards, e.g. cat *.rb (or even ccat *.rb)? Currently that gives:

$ ccat *.rb
Please, use one of the two syntaxes for invocation: 
source-highlight [OPTIONS]... -i input_file -o output_file
source-highlight [OPTIONS]... [FILES]...

and it would be great to be able to do:

ls *.rb | xargs ccat # (or cat)

the same way I can do:

ls *.rb | xargs cat

Currently I get:

$ ls *.rb | xargs ccat
xargs: ccat: No such file or directory
  • 1
    What would you like the colors to be dictated by? Log files or source files? It's difficult to colorize non-standard output. But there are ways. – slm Nov 12 '13 at 2:13
  • +1 By whether or not source-highlight can colorize that file type. – Michael Durrant Nov 12 '13 at 2:15
  • 'sudo nano <file>' worked a treat too. – Kickaha Nov 15 '17 at 0:26

Perhaps you could do something like the following script (untested):


for fn in "$@"; do
    source-highlight --out-format=esc -o STDOUT -i $fn 2>/dev/null || /bin/cat $fn

This does a few things:

  • iterates through each command line argument
  • tries to run source-highlight, redirecting error output to /dev/null
  • if source-highlight fails, then run regular /bin/cat

You put this script in a file named cdc for example, then alias cat=cdc.

As a function

You can adapt the above script into a Bash function call which can then be incorporated into your dot files like so:

cdc() { 
  for fn in "$@"; do 
    source-highlight --out-format=esc -o STDOUT -i $fn 2>/dev/null || /bin/cat $fn;

Edit (Michael) for some reason trying to use ccat for the function name didn't work but cdc did!

  • + I like the approach. Any way to avoid having the script though? I try to avoid having 'dependencies' in my working setup so I can switch and setup machines easily. I try to limit myself to my 'dotfiles', e.g. .bashrc, .bash_aliases, etc. – Michael Durrant Nov 12 '13 at 2:50
  • 1
    @MichaelDurrant - you could make this a function. See my edits. – slm Nov 12 '13 at 3:26
  • Thanks for this example BTW, I'm adding it to my environment! – slm Nov 12 '13 at 3:30
  • 1
    you are very welcome SO knowledge rocks! – Michael Durrant Nov 12 '13 at 3:30
  • hmmm, but I get -bash: /home/durrantm/.bashrc: line 31: syntax error near unexpected token ('` – Michael Durrant Nov 12 '13 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.