53

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?

5 Answers 5

54

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

8
  • Is that the Star Trek: Deep Space Nine font in their logo? If it is that's awesome. Commented Aug 26, 2011 at 3:57
  • 2
    Available as package python-pygments on Debian. Commented Aug 26, 2011 at 7:57
  • 8
    I found alias pcat="pygmentize -f terminal256 -O style=native -g" to be a nice solution Commented Dec 26, 2012 at 7:51
  • 1
    also available via pip install pygments Commented Nov 8, 2013 at 20:59
  • 1
    Actually it must be cat code.extension | pygmentize -f terminal -l extension. Commented Apr 16, 2015 at 21:34
15

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

1
  • Nice, thank you for taking the time to write up this answer Commented Nov 5, 2011 at 7:39
7

Highlight is simple to use and faster than pygmentize

1
  • 4
    Just don't forget to use -O ansi: unlike pygmentize, highlight outputs HTML by default. Commented Sep 20, 2017 at 15:26
2

I use vimcat.

https://github.com/ofavre/vimcat

vimcat example

It looks good enough for me.

1
  • 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! Commented Mar 29, 2020 at 0:40
1

Based on @Mikael Öhman's comment on @Ignacio Vazquez-Abrams's answer, I wrote this Python program to use pygments if possible or else use cat.

#! /usr/bin/env python3

from argparse import ArgumentParser
import subprocess
import os
import os.path


def main():
    parser = ArgumentParser(description='cat alternative that uses ANSI characters')
    parser.add_argument('file', help='the file to print out the contents of', type=str)
    args = parser.parse_args()
    if not os.path.isfile(args.file):
        parser.error(f'file not found: {args.file}')
    cat = False
    result = subprocess.run(('pygmentize -f terminal256 -O style=native ' + args.file).split(), stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    if result.stderr:
        cat = True
    commands = {
        True: 'cat ',
        False: 'pygmentize -f terminal256 -O style=native '
    }
    os.system(commands[cat] + args.file)


if __name__ == '__main__':
    main()

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.