less + Pygments
June 25, 2015
Syntax highlighting is awesome; colours convey extra information (thanks to Kurt for convincing me!). So of course it would be awesome to have
less do it too when using a terminal.
less provides a hook via the
LESSOPEN environment variable. And Pygments provides nice-looking syntax highlighting (this Web site uses it too).
# change to your favourite style export SOURCE_HIGHLIGHT_STYLE=/home/.../esc-solarized.style # obviously, you'll need to arrange for "pygmentize" to be installed; # consult your distribution's package manager, or PyPI. export LESSOPEN="| pygmentize -f terminal -O style=native -g %s" # I find I need -X export LESS='-FXR' # ...but do *not* want -X for man-pages export MANPAGER="less -+X -is"
less to pipe every file you page through
pygmentize. Occasionally, you may run across a file that Pygments can’t deal with properly; no problem just use:
cat file |
less. less doesn’t use the LESSOPEN thing when it’s getting data piped to it.
Of course, you’re not done yet! You’ll want to get Solarized working and make your terminal-emulator of choice use Source Code Pro but that’s for another post I suppose.
p.s. Thanks to Kurt for help getting this working…