I had the same question and I landed hear, read all answers and the following is what I finally did to have syntax highlighting everytime I use less
or most
:
sudo apt install source-highlight
Then I went and created an alias for my less
and most
(I use ZSH, but you can add aliases for your .bashrc
file if you use bash):
vim ~/.zshrc
(For those who might get stuck in vim: press i to go to edit mode, when you are done press Esc then : then x and finally
Enter to save the changes)
And paste the following lines for debian-based distros (like Debian, Ubuntu, Mint, etc.):
alias most='AliasFuncLess() { unset -f AliasFuncLess; $(dpkg -L libsource-highlight-common | grep lesspipe) "$1" | less -R };AliasFuncLess'
alias most='AliasFuncMost() { unset -f AliasFuncMost; $(dpkg -L libsource-highlight-common | grep lesspipe) "$1" | most };AliasFuncMost'
or for RPM distros (Redhat, Fedora, Centos):
alias most='AliasFuncLess() { unset -f AliasFuncLess; $(rpm -ql source-highlight | grep lesspipe) "$1" | less -R };AliasFuncLess'
alias most='AliasFuncMost() { unset -f AliasFuncMost; $(rpm -ql source-highlight | grep lesspipe) "$1" | most };AliasFuncMost'
Remember to close the terminal and open it again to make sure alias is created.
Now you can enjoy the colors:
most yourFile.xml
23On Debian:
sudo apt-get install source-highlight
,export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
,export LESS=' -R '
– Morgan Courbet – 2014-08-22T08:57:40.3203small caveat: source-highlight does not currently support markdown. – dhulihan – 2016-07-07T06:10:22.233
also
alias more='more -R '
ormore
will stop working. – Brian – 2017-02-08T01:25:58.050As @dhulihan wrote, source-highlight does not support markdown. But highlight supports markdown and many more. OSX:
– ronen – 2017-05-26T22:26:44.123brew install highlight
;export LESSOPEN="| /usr/local/bin/highlight --out-format=xterm265 %s"
Note that the Debian packaging is actually a bit broken: The script itself is in
libsource-highlight-common
, but callssource-highlight
from eponymous package, but does not declare dependency on it (split as they are now, they seem to actually form a circular dependency). – Alois Mahdal – 2017-10-20T00:54:23.080For Raspbian installs, use
export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
. – steviethecat – 2018-01-26T08:53:11.727After installation and setting the env var, I still get no syntax coloring in less, unless I set the env vars in
.bashrc
, strange that setting it in/etc/profile.d/less.sh
does not work; the vars are read and I can see them loaded(addedecho
in/etc/profile
to see which files are loaded). Someone to help? It's a login shell with putty. – WesternGun – 2018-02-14T09:58:59.913source-highlight
requires a file (a named pipe won't work) for syntax highlighting, so something likepipeline | less
won't work. For a solution see this answer. – Tom Hale – 2018-06-17T16:41:58.190Is it possible to combine this with the default lesspipe(.sh) somehow? – AstroFloyd – 2018-10-07T06:50:16.640
@dhulihan GNU source-highlight - Bugs: bug #46542, Add support for Markdown We are all welcome to send a pull request
– Pablo A – 2019-02-01T17:56:49.910This works fine but it breaks the ability to use default
– Treviño – 2019-03-11T14:39:34.433lesspipe
for binary files, so instead I'd suggest something that uses this solution only for binary files (i.e. something like https://gist.github.com/textarcana/4611277#gistcomment-2859093), or just another script that does the same.I retrieve the following error message when I run
less ~/.zshrc
. Error:src-hilite-lesspipe.sh: line 9: source-highlight: command not found
. I run this on MacOSX. – JJD – 2012-06-04T16:38:06.0872@JJD Did you install source-highlight? – Max Nanasy – 2012-08-31T06:00:14.007
13OSX:
brew install source-highlight
;export LESSOPEN="| /usr/local/bin/src-hilite-lesspipe.sh %s"
. Note the path change. – Gregg Lind – 2014-04-22T19:26:19.333