Syntax Highlight Command Line Tool


As a software engineer I found myself pretty often in needs of showcasing a piece of code in a document or presentation. While coding, we have IDE and editor tools like VSCode for highlighting the syntax, to make it more readable. However, for the software not designed for developers, usually there’s no such syntax highlighting feature for code. I think explaining your code and idea is critical for software engineering, so syntax highlighting in documents and presentations is as important as for development. With that in mind, I wrote a piece shell script that helps you syntax highlighting code snippets for MacOS:

function shl() {
    pbpaste | pygmentize -l $1 -f html -O style=monokai -O full=True > /tmp/highlighted-code.html
    open /tmp/highlighted-code.html
}

To use it, you need to install Pygments first, you can run

pip install Pygments

Next, all you need to do is put the piece of shell script into your ~/.bash_profile. You can now reload the bash profile via

source ~/.bash_profile

Then you can then copy the code you want to highlight and run this

shl shell

And here you go!

Output result of syntax highlight html page

It will output an HTML file in temporary folder and open it. As long as your editor supports copy paste from browser, you should now be able to copy the code and paste it wherever you want. And you can change the shell to whatever programming language Pygments supports, like cpp, python:

shl python
shl cpp
# ...

Now, with this script, it’s super simple. Just copy the code, run the command, and that’s it. Hope this is helpful, enjoy code highlighting for your doc writing and presentation :)


This article is sponsored by nobody but myself 🤣

Hire me

Interested in hiring a one-person army software engineer to build your next dream product? Do you need an excellent team-player software engineer who can fill almost any role before filling the gap? Check out my work and my GitHub profile to see some of them. I have more than 20 years of experience building software in various fields. I provide one-stop, high-quality, rapid software development services from end to end, be it mobile, front-end, back-end, DevOps, data, or security. You can reach out to me at [email protected].

Contact me
This ad is powered by PolisNetwork

Recent articles:

Nvidia GPU on bare metal NixOS Kubernetes cluster explained
MAZE - My AI models are finally evolving!
MAZE - How I would build AGI