Description
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 $argv
end
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.)