-
-
Save textarcana/4611277 to your computer and use it in GitHub Desktop.
# 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 |
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"?
Use Vim as PAGER https://github.com/rkitover/vimpager
The problem with src-hilite-lesspipe
and friends is that if you use https://github.com/wofr06/lesspipe so that it opens binary files, using a such LESSOPEN breaks it.
So I've defined something like in my .bashrc
:
lesscolors()
{
env LESSOPEN='| /usr/share/source-highlight/src-hilite-lesspipe.sh %s' \
LESS=' --RAW-CONTROL-CHARS ' \
$(which less) $@
}
less()
{
less=$(which less)
has_file=false
all_text=true
for arg in $@; do
if [[ "$arg" != -* ]] && [ -e "$arg" ]; then
has_file=true
enc=$(file -b --mime-encoding "$(realpath $arg)")
if [[ "$enc" != *"-ascii" ]] && [ "$enc" != "utf-8" ]; then
all_text=false
break
fi
fi
done
if [ $has_file == true ] && [ "$all_text" == true ]; then
less=lesscolors
fi
$less $@
}
It can indeed make it more complex, but it works for most of cases, so it will allow to use both default lesspipe (i.e. to open pdf or zip files) while it will colorize the text ones.
echo 'good luck with that' | less
On fish shell
what worked for me was:
$ brew install source-highlight
$ vim ~/.config/fish/functions/less.fish add the following content
function less
src-hilite-lesspipe.sh $argv | less -N
end
$ source ~/.config/fish/config.fish
$ less
For dark displays use the esc256 style in src-hilite-lesspipe.sh
:
#! /bin/bash
for source in "$@"; do
case $source in
*ChangeLog|*changelog)
source-highlight --failsafe -f esc256 --lang-def=changelog.lang --style-file=esc256.style -i "$source" ;;
*Makefile|*makefile)
source-highlight --failsafe -f esc256 --lang-def=makefile.lang --style-file=esc256.style -i "$source" ;;
*.tar|*.tgz|*.gz|*.bz2|*.xz)
lesspipe "$source" ;;
*) source-highlight --failsafe --infer-lang -f esc256 --style-file=esc256.style -i "$source" ;;
esac
done
( sed 's,esc,esc256,g'
will help ;)
2020 update I think that bat(1) replaces all of this and that one should use bat
going forward instead of the less / source-highlight combo. https://github.com/sharkdp/bat#syntax-highlighting
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.