43

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?

48

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

| 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
  • 6
    I found alias pcat="pygmentize -f terminal256 -O style=native -g" to be a nice solution – Mikael Öhman Dec 26 '12 at 7:51
  • 1
    also available via pip install pygments – craigb Nov 8 '13 at 20:59
  • 1
    Actually it must be cat code.extension | pygmentize -f terminal -l extension. – fiatjaf Apr 16 '15 at 21:34
13

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.)

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

Highlight is simple to use and faster than pygmentize

| improve this answer | |
  • 3
    Just don't forget to use -O ansi: unlike pygmentize, highlight outputs HTML by default. – Ruslan Sep 20 '17 at 15:26
2

I use vimcat.

https://github.com/ofavre/vimcat

vimcat example

It looks good enough for me.

| improve this answer | |
  • I like this idea to use my existing Vim highlighting, just wish the utility worked on all *nix and was available for MacOS via Homebrew! – camflint Mar 29 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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