A more colourful cat in your shell

Posted August 6th, 2013 • 2 min read

I just wanted to quickly share a shell alias I have been using lately to get some more color in the terminal when you cat a file.

Below is a screenshot of what an average terminal looks like when you just use

$ cat somefile.js

Terminal Dull Colours

Sure, it does the job, and you probably are ok with it looking dull as hell. But what if you could make it look like this:

Terminal Colour

Indeed! If you could, why wouldn't you?

The ingredients

So, let's get to it. Luckily this isn't very complex to get going on your machine. Let's start by installing Pygments. This little Ruby wrapper library is used to make the realtime syntax highlighting possible: bash

gem install pygments

Next up, the alias itself. Depending on your shell and environment open up your profile. If you aren't sure whether you've installed or configured something specifically on your system, chances are the file you are looking for (on OSX at least) is ~/.bashrc.

In there, add the following alias:

alias c='pygmentize -O style=monokai -f console256 -g'

And that's all there is to it. Now whenever you type c some-file.ext it will detect the extension and add syntax highlighting.

I chose the monokai style because it looks awesome and works well with the rest of my ZSH theme, but feel free to try a few and see which works best for you.

Happy CLI-ing!

Stay up to date

Want to know when a new post comes out and stay in the loop on tips, tricks and gotchas? Consider signing up for the Mindthecode newsletter.

Comments

Keep reading

October 26th, 2012 • 2 min read
My experiences while converting this blog to an Octopress one
December 6th, 2011 • 2 min read
Loading external assets with curl is not only easy, it's also a lot faster than file_get_contents
December 8th, 2013 • 1 min read
My experience while rebuilding this blog with CabinJS and Grunt