How to syntax highlight via Less

lesssyntax highlighting

is there a way to syntax highlight a file I see in Less?

actually I'm using this command to open an xml file (and sometimes a series of them)

less htmleditors/htmleditors_config.xml

or

less [multiple files]

I'd like to stay in Less (to learn that program better and to use my knowledge of :n and :p for next/previous navigation)

But it also want some kind of basic syntax highlighting – at least show the comments differently. Do you know any way to do it?

Best Answer

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
    
Related Question