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

Syntax-highlighted “less” in command line

When using the command “less”, you want to get a syntax-highlighted output.

Following this article, here is my working solution for Ubuntu.

Edit your ~/.bashrc file and add the following line:

# for colored less
export LESS='-R'

This way the raw ANSI color codes pass through.

Then create the file ~/.lessfilter with the following content:

# .lessfilter to use pygmentize

case "$1" in
# add all extensions you want to handle here
pygmentize -f terminal256 -O style=native -g "$1"; exit 0;;

exit 1;

The command pygmentize is in the package “python-pygments“.

And don’t forget to make it executable!

chmod u+x ~/.lessfilter

Done. Open a new terminal and try it out.

Thanks to rajbot who forwarded me to the original article.

Categories: bash, python Tags: , ,
  1. Hugo Holgersson
    October 30, 2015 at 10:32

    This only works if you do “less some_source_file.c”. It does not work, when piping a text into less. For example: “ag TEXT | less”

  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

%d bloggers like this: