If I want to output a C source code file with syntax highlighting, can I use cat?


A GNU package, source-highlight, seems to do the trick (though isn't using cat -- as John T points out, this isn't possible with cat specifically). It's available via apt-get on Ubuntu, and requires the Boost regex library. Check your package manager to see if both are available, otherwise you can grab them from the web. The GNU page linked earlier has a link to Boost, I think.

After installation, I created a new script in my path called ccat. The script looks like:

src-hilite-lesspipe.sh $1

Nothing fancy, just simplifying the less script they include with source-highlight. It acts just like cat when called in this fashion.

The included less script is a nice script to use as well, though. I just added the following to .bashrc:

export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=' -R '

That script is included in the online manual for source-highlight, as well.

I guess you could alias cat to call src-hilite-lesspipe.sh $1 if you felt like ignoring cat altogether, but that might not be desirable.

  • 4
    I, personally, prefer an alias to one-line bash scripts ending with $1 :). – mgalgs Sep 29 '11 at 15:55
  • Yep, mitch_feaster, you're spot on. :] – rgm Dec 23 '11 at 20:56
  • 2
    Works on a Mac too with brew install source-highlight . Unable to color yaml :( – Sairam Jan 22 '13 at 7:40

To output syntax highlighted code with something like cat, I created a ccat command by following the instructions at http://scott.sherrillmix.com/blog/programmer/syntax-highlighting-in-terminal/.

if [ ! -t 0 ];then
elif [ -f $1 ];then
  echo "Usage: $0 code.c"
  echo "or e.g. head code.c|$0"
  exit 1
pygmentize -f terminal -g $file

To output syntax highlighted code with something like less, I use vim as a less replacement.

alias less='/usr/share/vim/vim72/macros/less.sh'
  • How to install pygmentize: easy_install --install-dir $SOMEDIR Pygments – psihodelia Aug 21 '13 at 7:14
  • I like this type of portable solutions. I added this script as a function to my bash .functions as colorize(){...} – guneysus Dec 27 '13 at 12:46

To solve this, I used highlight. I made a function that tries to print the file with syntax highlighting, and if it fails it falls back to simply using cat to print the file. You can change the syntax highlighting theme to whatever you want.

function hl { # Overrides the cat command to use syntax highlighting
    # Highlight with 'moria' theme to terminal, and suppress errors
    highlight $1 -s moria -O xterm256 2> /dev/null

    if (($? != 0)); then # If the command had errors
        cat $1 # Just cat the file out instead

If you're on a Mac and you use Homebrew (highly recommended!), you can install highlight by running brew install highlight. Otherwise, it should be available on most other package managers and can be downloaded here.

I also made a function to print out a file with syntax highlighting as html and open it in the browser to print (relies on the open command on OS X):

function hlprint {
    # Print with line numbers and 'moria' theme
    highlight $1 -l -o print.html -s moria
    open print.html # Open in browser
    sleep 5 # Give the browser time to open
    rm print.html highlight.css # Remove output files



No, cat has no syntax highlighting abilities. If you'd like to view source code with syntax highlighting, pop it into vim or your editor of choice (that has syntax highlighting). This way, you can even page through the output if it's a long file using Ctrl + F (forward) and Ctrl + B (backwards).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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