You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using pygments to do source highlighting on the command line. I created an alias for lessh, so less that does source code highlighting. Sure, I could use a text editor like nano or vim, but I don't want there to be a chance that I'll edit the source file.
Bash Shell
I use a dark terminal environment, which works for "native" style. Choose whichever style you prefer.
This uses the less environment variable LESSOPEN to pre-process the input file(s):
alias lessh='LESSOPEN="| pygmentize -f 256 -O style=native %s" less -M -R '
Default style version:
alias lessh='LESSOPEN="| pygmentize %s" less -M -R '
You could set the LESSOPEN variable globally and use less, but this will pipe every single file through pygmentize. That will make less run slower because it will always run files through pygmentize.
Fish Shell
The fish shell equivalents is to create functions, funced lessh is used to create the function. Test it out. Then, save it to the fish shell with the funcsave lessh.
function lessh
LESSOPEN="| pygmentize -f 256 -O style=native %s" less -M-R$argvend
Note: I also made cath, which is basically alias cath="pygmentize". Which doesn't really work like cat, so it does not concatenate the files and highlight. It just outputs a file to the screen highlighted. I should choose a different alias for this.
I thought someone else might be able to benefit from this. Could this be added to the documentation as a way to use pygments? (GNU Source-highlight documented a something similar, which I was inspired by.)
Activity
amitkummer commentedon May 8, 2022
I like the idea, and can confirm the
lesshalias also works onZSH, as I have added it to my personal setup :).I can create a PR, just would like to have approval from one of the maintainers first, if adding this to the documentation sounds like a good idea.
jeanas commentedon Aug 18, 2022
SGTM.
Add Terminal Tips Documentation (#2425)
micahcochran commentedon May 7, 2023
Closing. Fixed by merging PR #2425.