Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a variant of cat that outputs syntax-highlighted lines of code when used on a source file?

An idea: maybe vi[m] or another editor can be asked to dump the syntax-highlighted contents of said files to stdout and exit immediately?

share|improve this question

2 Answers 2

up vote 26 down vote accepted

Passing the file through pygmentize-f terminal will attempt to detect the type from the filename and highlight it appropriately.

share|improve this answer
    
Is that the Star Trek: Deep Space Nine font in their logo? If it is that's awesome. –  amphetamachine Aug 26 '11 at 3:57
2  
Available as package python-pygments on Debian. –  enzotib Aug 26 '11 at 7:57
    
I found alias pcat="pygmentize -f terminal256 -O style=native -g" to be a nice solution –  Mikael Öhman Dec 26 '12 at 7:51
    
also available via pip install pygments –  craigb Nov 8 '13 at 20:59
    
@MikaelÖhman I actually found it easier to alias cat to a function pcat that took the filename as an argument, and output that to less: stackoverflow.com/questions/1821495/… –  Droogans Jun 3 at 18:53

The Source-highlight package is shipped with the esc.outlang output language definition, which highlights with ANSI escape sequences.

A handy wrapper src-hilite-lesspipe.sh is also included in the package, so displaying highlighted output in the terminal is just src-hilite-lesspipe.sh source.file.

Actually src-hilite-lesspipe.sh's primary reason is to help automating the use of source-highlight with less. You just set:

export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=' -R '

Then any less source.file will show highlighted source code. (Code in unknown language will pass through unaltered. Highlighting will be also skipped in case of redirected content, like less < source.file.)

share|improve this answer
    
Nice, thank you for taking the time to write up this answer –  Paolo Nov 5 '11 at 7:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.