Syntax highlighting in less
If you have had to read text based files in Linux environment, you most likely have stumbled upon a tool called less
. It’s very powerful tool, but I quite often wish there’d some level of syntax highlighting when viewing e.g. shell scripts or something similar.
Today I decided to research the topic a bit and found out it is indeed possible to use different styles with less. The most feasible option for me was to use external tool called pygmentize
. It is a generic syntax highlighter created with Python.
Here’s how I made less
to use syntax highlighting offered by pygmentize
always when I use less
.
- Install
pygmentize
by runningsudo apt install python3-pygments
- Select your favorite theme from the built-in themes (you could also download or create your own). For this post I’ll use my favorite: one-dark
- Add
export LESSOPEN="|pygmentize -P style=one-dark -g %s
andexport LESS="-R"
to your shell profile file (e.g. for bash:~/.bashrc
) - To use in your current shell session, remember to run
source <shell profile file>
, for example on bashsource ~/.bashrc
- instead of this you could also just restart your terminal - Open a file with
less
- it should have syntax highlighting now!
After finding out it is this simple, I wish I had searched for this much earlier since this is a really good improvement 😅
Thanks for reading and enjoy your more colorful less
!
Let me know what you think of this article on twitter @niklas1e or leave a comment below!
Latest Articles
- Syntax highlighting in less March 2, 2024
- Satoshi Nakamoto potentially solved the crypto trilemma March 24, 2023
- Help! I forgot my WSL password September 19, 2022
- Why my Bash script ran in yarn/npm is throwing errors? February 8, 2022
- Due diligence: Beaxy February 13, 2021
- more...