Utilizing emacs' or vim's syntax highlighter for command-line program?

emacs, pygments, syntax-highlighting, vim

Solution

as the editor can already do the ansi stuff, rather easy to make a screen capture of the editor, no?

script -qc "stty rows 10000
emacs -nw -visit YOURFILE.YAML -eval '(redisplay t)' -f kill-emacs
resize"

(redisplay is only needed for GNU FSF Emacs)

now clean up the capture

perl -p0E 's/\A(?s:.*)\e\[27m\r\n
\e\[A\n((?s).*?)
(?:\e\[K\n)+
\e.*\e\[27m\r$(?s:.*)\Z/$1/mx' < typescript

if you don't want the recording process visible on screen, you can wrap it in a hidden terminal with something like perl's IO::Pty

Problem

I have a command-line program that spews JSON and YAML. By default it detects if pygments (pygmentize) is available and if it does, pass the output throught it to get a nice colorized output. However, pygments is not installed by default on most computers that this program will run on. But most computers will have either emacs or vim, however, does. Is there a way to get one of these editors to syntax-highlight some text using ANSI escape sequences and then output it again?

Original source