How to syntax highlight via Less

You can use GNU's source-highlight, as shown here (path may differ, see below):

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

As of Debian Stretch and Fedora 25, package names and script paths differ

  • Debian:

    sudo apt install libsource-highlight-common source-highlight
    dpkg -L libsource-highlight-common | grep lesspipe
    # /usr/share/source-highlight/src-hilite-lesspipe.sh
    
  • Fedora:

    sudo dnf install source-highlight
    rpm -ql source-highlight | grep lesspipe
    # /usr/bin/source-highlight/src-hilite-lesspipe.sh
    

Best of both previous answers: you can invoke system default editor from within less, by pressing v.


pygmentize somefile.ex | less -R

or

function cless () {
    pygmentize -f terminal "$1" | less -R
}

Pygmentize comes as a part of the Pygments Python syntax highliter.