Sunday, May 5, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 2245  / 3 Years ago, tue, july 27, 2021, 10:05:27

I often use the command pygmentize filename.py to show color source files with code formatting. This works like a charm. I now also want to show line numbers like cat -n filename.py 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

 Answers
2

You can add line numbers using perl like this:



pygmentize file.py | 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 file.py.


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

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

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