Last Updated: September 09, 2019
· tomjakubowski

syntax highlighting in the terminal with pygments

the Pygments syntax highlighting library comes with a CLI tool 'pygmentize', which has a formatter for terminal color codes:

pygmentize -f terminal

you can pipe it into less to paginate output:

pygmentize -f terminal | less -R

and then set an alias for dead-simple CLI usage (example is for zsh):

pretty() { pygmentize -f terminal "$1" | less -R }

you can also use the 'terminal256' formatter for a 256 color terminal.


4 Responses
Add your response

That's cool, already add alias to my .zshrc :)

over 1 year ago ·

You are probably better off with the following function

pretty() { pygmentize -f terminal $* | less -R }

Gives you more freedom to pass options like

pretty -l sh .zshrc   
over 1 year ago ·

Why not adding -g to allow auto detection of lexer ;-) ?

over 1 year ago ·

pless() { less -f terminal256 -g -P style=monokai $* | less -FiXRM }

over 1 year ago ·