Home > bash, python > Syntax-highlighted “cat” in command line

Syntax-highlighted “cat” in command line

Problem
You have a source code whose content you want to display on the stdout. The natural way to do that is the “cat” command but it’s monochrome. How to get a syntax-highlighted output?

Solution
Use pygments. If you install it (“sudo apt-get install python-pygments“), you will have a command called “pygmentize” that you can invoke from the command line.

Example:

pygmentize -f terminal256 -O style=native -g color_me.py

Tip: put an alias on it.

alias pcat="pygmentize -f terminal256 -O style=native -g"

There are different styles available, see “pygmentize -L” for a list.

Tip from here. Discussion @reddit here.

Links

About these ads
  1. May 29, 2013 at 17:32

    good stuff, thanks.

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 72 other followers

%d bloggers like this: