Adds syntax highlighting to everyone's favorite terminal pager, less
.
These instructions assume an Ubuntu-based distro; modify as needed.
Pygments provides pygmentize
. Your system may already have an outdated version of Pygments installed. If so, leave that alone and install the latest Pygments locally, giving it priority in your $PATH
. Pipx can facilitate these two tasks.
You'll also need awk
:
# install pipx if needed
sudo apt install pipx
# add pipx-installed binaries to `$PATH` if not already
pipx ensurepath
# install Pygments and GNU awk
pipx install Pygments
sudo apt install gawk
Most Linux distros already have lesspipe
enabled, but you can check for certain by running:
echo $LESSOPEN
If you don't see lesspipe
or lessfile
in the output, install lesspipe.
Add the following to ~/.bashrc
:
# sets LESSOPEN and LESSCLOSE variables
eval "$(SHELL=/bin/sh lesspipe)"
# interpret color characters
export LESS='-R'
# more styles available, see: `pygmentize -L styles`
export PYGMENTIZE_STYLE='paraiso-dark'
If you opted out of lesspipe
in the previous step, replace the above eval
statement with:
export LESSOPEN='|~/.lessfilter %s'
This repo contains a pre-generated .lessfilter which is currently at version 2.18.0
and is updated occasionally. You could use that and skip to the next step, even if its version lags behind that of Pygments (any unsupported file types would fallback to plain-text).
You could also generate a .lessfilter
yourself by running main.py, which scrapes the Pygments lexer documentation website and produces a .lessfilter
in this directory which corresponds to the latest published version:
git clone https://github.com/CoeJoder/lessfilter-pygmentize.git
cd lessfilter-pygmentize/
pipenv install
pipenv run python main.py >/dev/null
# if you performed step 4, do this:
cp .lessfilter ~
# otherwise, do this:
wget -P ~ https://github.com/CoeJoder/lessfilter-pygmentize/raw/master/.lessfilter
# now make it executable
chmod +x ~/.lessfilter
That's it. Test it out by running less ~/.lessfilter
.