2024-06-15 20:00:03 -07:00
2024-06-15 20:00:03 -07:00
2024-06-15 17:23:19 -07:00
2024-06-15 17:23:19 -07:00
2024-06-15 20:00:03 -07:00
2021-02-11 11:27:58 -08:00

Syntax highlighter for less

Adds syntax highlighting to everyone's favorite terminal pager, less. screenshot

Installation

These instructions assume an Ubuntu-based distro; modify as needed.

1. Install pygmentize & awk

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.

3. Configure Environment Variables

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'

# optional
alias ls='ls --color=always'
alias grep='grep --color=always'

If you opted out of lesspipe in the previous step, replace the above eval statement with:

export LESSOPEN='|~/.lessfilter %s'

4. Generate ~/.lessfilter (optional)

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

5. Copy ~/.lessfilter to $HOME and make it executable

cp ./.lessfilter ~
chmod +x ~/.lessfilter

That's it. Test it out by running less ~/.lessfilter.

Description
A pygments-based syntax highlighter for less.
Readme 159 KiB
Languages
Python 76.9%
Shell 23.1%