Monday, September 25, 2023
rated 0 times [  3] [ 0]  / answers: 1 / hits: 2165  / 2 Years ago, tue, july 27, 2021, 10:05:27

I often use the command pygmentize to show color source files with code formatting. This works like a charm. I now also want to show line numbers like cat -n does.

I read around and found some suggestions like adding -O linenos=1 or -O linenos=True. None of this seems to work though.

Does anybody have any idea how I can show line numbers when viewing source files on the command line with pygments? All tips are welcome!

More From » command-line


You can add line numbers using perl like this:

pygmentize | perl -e 'print ++$i." $_" for <>'

Update (to alias the previous command):

To create an alias, just define the following bash function somewhere in your ~/.bashrc:

pyg() {
pygmentize $1 | perl -e 'print ++$i." $_" for <>'

Open a new terminal and call the command with pyg

[#24197] Thursday, July 29, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 33
Total Questions: 136
Total Answers: 114

Location: Singapore
Member since Wed, Jan 13, 2021
3 Years ago