Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to enable syntax-highlighting in less. Use `less -N` (or type -N while in less) to enable line numbers. Based on the procedure described in http://superuser.com/questions/71588
# Enable syntax-highlighting in less.
# Last tested on CentOS 6.3.
#
# First, add these two lines to ~/.bashrc
# export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s"
# export LESS=" -R "
sudo yum -y install boost boost-devel ctags
wget http://springdale.math.ias.edu/data/puias/unsupported/6/x86_64/source-highlight-3.1.6-3.puias6.x86_64.rpm
source-highlight-3.1.6-3.puias6.x86_64.rpm
sudo yum -y install source-highlight
# Enable syntax-highlighting in less.
#
# First, add these two lines to ~/.bashrc
# export LESSOPEN="| /opt/local/bin/src-hilite-lesspipe.sh %s"
# export LESS=" -R "
sudo port install source-highlight
@textarcana

This comment has been minimized.

Show comment Hide comment
@textarcana

textarcana Apr 19, 2013

And here's how to install source-highlight with HomeBrew (thx Kastner):

brew install source-highlight

Owner

textarcana commented Apr 19, 2013

And here's how to install source-highlight with HomeBrew (thx Kastner):

brew install source-highlight

@mfalkvidd

This comment has been minimized.

Show comment Hide comment
@mfalkvidd

mfalkvidd Oct 17, 2013

Awesome!
I installed source-highlight using the cygwin installer, works great in mintty on Windows.

Awesome!
I installed source-highlight using the cygwin installer, works great in mintty on Windows.

@akgerber

This comment has been minimized.

Show comment Hide comment
@akgerber

akgerber Jan 8, 2014

This is great! Note that, for homebrew, the script is located at:
/usr/local/bin/src-hilite-lesspipe.sh

akgerber commented Jan 8, 2014

This is great! Note that, for homebrew, the script is located at:
/usr/local/bin/src-hilite-lesspipe.sh

@rubytester

This comment has been minimized.

Show comment Hide comment
@rubytester

rubytester Jan 13, 2014

Neat. on mac os x I have added the following to my less setup in .bashrc

### LESS ###
# Enable syntax-highlighting in less.
# brew install source-highlight
# First, add these two lines to ~/.bashrc
export LESSOPEN="| /usr/local/bin/src-hilite-lesspipe.sh %s"
export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
alias more='less'

Thanks for the pointer to this. very helpful

Neat. on mac os x I have added the following to my less setup in .bashrc

### LESS ###
# Enable syntax-highlighting in less.
# brew install source-highlight
# First, add these two lines to ~/.bashrc
export LESSOPEN="| /usr/local/bin/src-hilite-lesspipe.sh %s"
export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
alias more='less'

Thanks for the pointer to this. very helpful

@nachoaIvarez

This comment has been minimized.

Show comment Hide comment
@nachoaIvarez

nachoaIvarez Aug 10, 2015

@rharriso

This comment has been minimized.

Show comment Hide comment
@rharriso

rharriso Jan 13, 2016

A little different in on my arch ~/.zshrc

### LESS ###
# Enable syntax-highlighting in less.
# brew install source-highlight
# First, add these two lines to ~/.bashrc
export LESSOPEN="| src-hilite-lesspipe.sh %s"
export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
alias more='less'

A little different in on my arch ~/.zshrc

### LESS ###
# Enable syntax-highlighting in less.
# brew install source-highlight
# First, add these two lines to ~/.bashrc
export LESSOPEN="| src-hilite-lesspipe.sh %s"
export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
alias more='less'
@F1LT3R

This comment has been minimized.

Show comment Hide comment
@F1LT3R

F1LT3R Feb 18, 2016

That's awesome.

I've set mine up with highlight (instead of source-highlight) like this:

### Terminal Syntax Highlighting

# Setup: "brew install highlight"

# Pipe Highlight to less
export LESSOPEN="| $(which highlight) %s --out-format xterm256 --line-numbers --quiet --force --style solarized-light"
export LESS=" -R"
alias less='less -m -N -g -i -J --line-numbers --underline-special'
alias more='less'

# Use "highlight" in place of "cat"
alias cat="highlight $1 --out-format xterm256 --line-numbers --quiet --force --style solarized-light"

# Setup JSON Syntax Highlighting
# Copy js.lang to json.lang with the following command
# cp "$(dirname $(brew list highlight | head -n 1))/share/highlight/langDefs/js.lang" "$(dirname $(brew list highlight | head -n 1))/share/highlight/langDefs/json.lang"

I found highlight gave me better options and control over the output.

F1LT3R commented Feb 18, 2016

That's awesome.

I've set mine up with highlight (instead of source-highlight) like this:

### Terminal Syntax Highlighting

# Setup: "brew install highlight"

# Pipe Highlight to less
export LESSOPEN="| $(which highlight) %s --out-format xterm256 --line-numbers --quiet --force --style solarized-light"
export LESS=" -R"
alias less='less -m -N -g -i -J --line-numbers --underline-special'
alias more='less'

# Use "highlight" in place of "cat"
alias cat="highlight $1 --out-format xterm256 --line-numbers --quiet --force --style solarized-light"

# Setup JSON Syntax Highlighting
# Copy js.lang to json.lang with the following command
# cp "$(dirname $(brew list highlight | head -n 1))/share/highlight/langDefs/js.lang" "$(dirname $(brew list highlight | head -n 1))/share/highlight/langDefs/json.lang"

I found highlight gave me better options and control over the output.

@bezhermoso

This comment has been minimized.

Show comment Hide comment
@bezhermoso

bezhermoso Aug 18, 2016

Thanks for sharing your config @F1LT3R!

Thanks for sharing your config @F1LT3R!

@Fylwind

This comment has been minimized.

Show comment Hide comment
@Fylwind

Fylwind Sep 14, 2016

@F1LT3R Thanks! The Solarized color scheme of highlight is a lot better than the default of source-highlight, which had black text that was unreadable on a dark console.

Fylwind commented Sep 14, 2016

@F1LT3R Thanks! The Solarized color scheme of highlight is a lot better than the default of source-highlight, which had black text that was unreadable on a dark console.

@bennypowers

This comment has been minimized.

Show comment Hide comment
@bennypowers

bennypowers Oct 18, 2016

for aquatic shell jockeys

# ~/.config/fish/config.fish

set hilite (which highlight)
set -x LESSOPEN "| $hilite %s --out-format xterm256 --quiet --force "
set -x LESS " -R"
# ~/.config/fish/functions/less.fish
function less
    command less -m -N -g -i -J --underline-special --SILENT $argv
end

for aquatic shell jockeys

# ~/.config/fish/config.fish

set hilite (which highlight)
set -x LESSOPEN "| $hilite %s --out-format xterm256 --quiet --force "
set -x LESS " -R"
# ~/.config/fish/functions/less.fish
function less
    command less -m -N -g -i -J --underline-special --SILENT $argv
end
@almereyda

This comment has been minimized.

Show comment Hide comment
@almereyda

almereyda Jan 4, 2017

@F1LT3R Any ideas how to tell highlight also to interpret dotfiles, and only those, preferably as sh or a similar syntax?

@F1LT3R Any ideas how to tell highlight also to interpret dotfiles, and only those, preferably as sh or a similar syntax?

@mike3run

This comment has been minimized.

Show comment Hide comment
@mike3run

mike3run Mar 20, 2017

Actual way to make less work on fish shell since @bennypowers solution didn't work for me:

# ~/.config/fish/config.fish
# or
# ~/.config/omf/init.fish
# If you're using oh-my-fish
set hilite (which src-hilite-lesspipe.sh)
set -x LESSOPEN "| $hilite %s"
set -x LESS " -R -X -F "

mike3run commented Mar 20, 2017

Actual way to make less work on fish shell since @bennypowers solution didn't work for me:

# ~/.config/fish/config.fish
# or
# ~/.config/omf/init.fish
# If you're using oh-my-fish
set hilite (which src-hilite-lesspipe.sh)
set -x LESSOPEN "| $hilite %s"
set -x LESS " -R -X -F "
@aleshivan

This comment has been minimized.

Show comment Hide comment
@aleshivan

aleshivan Sep 18, 2017

First install on Linux Mint or Ubuntu

apt-get install source-highlight

add at end of on my arch ~/.zshrc also works on ~/.bashrc

# Enable syntax-highlighting in less.
# apt-get install source-highlight
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
alias more='less'

aleshivan commented Sep 18, 2017

First install on Linux Mint or Ubuntu

apt-get install source-highlight

add at end of on my arch ~/.zshrc also works on ~/.bashrc

# Enable syntax-highlighting in less.
# apt-get install source-highlight
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=" -R "
alias less='less -m -N -g -i -J --underline-special --SILENT'
alias more='less'
@WesleyBlancoYuan

This comment has been minimized.

Show comment Hide comment
@WesleyBlancoYuan

WesleyBlancoYuan Feb 20, 2018

At last I cannot put this in /etc/profile.d/xxx.sh. I have to put these lines into ~/.bashrc to take effect. Don't know why. I am with CentOS 7 and I am with Putty.

At last I cannot put this in /etc/profile.d/xxx.sh. I have to put these lines into ~/.bashrc to take effect. Don't know why. I am with CentOS 7 and I am with Putty.

@alexanderkjeldaas

This comment has been minimized.

Show comment Hide comment
@alexanderkjeldaas

alexanderkjeldaas Mar 5, 2018

So how do I avoid only being able to see the top of the file because of:

source-highlight: The complexity of matching the regular expression exceeded predefined bounds.  Try 
refactoring the regular expression to make each choice made by the state machine unambiguous.  This 
exception is thrown to prevent "eternal" matches that take an indefinite period time to locate.

printed to stderr?

Can there be some sort of "if highlighting fails, just do a normal less"?

alexanderkjeldaas commented Mar 5, 2018

So how do I avoid only being able to see the top of the file because of:

source-highlight: The complexity of matching the regular expression exceeded predefined bounds.  Try 
refactoring the regular expression to make each choice made by the state machine unambiguous.  This 
exception is thrown to prevent "eternal" matches that take an indefinite period time to locate.

printed to stderr?

Can there be some sort of "if highlighting fails, just do a normal less"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment