7

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?

4
  • BTW, I just found out about coderay. It's ruby-based and its command-line program is much faster than pygmentize. Nov 15, 2012 at 23:49
  • 1
    Another option would be GNU Source-highlight. I use this to get colors with less. Nov 16, 2012 at 12:11
  • @RandyMorris: yup, there's even a CPAN module for it (my program is Perl-based). The only problem is that the dependency is quite large (installing source-highlight and its dev libraries to build the CPAN module pulls about 100MB of Debian packages). Nov 22, 2012 at 2:49
  • I solved my problem by creating a JSON and YAML color dumper. Turns out I don't really need a syntax highlighter (blush). But I thank the advices in the answers. Nov 22, 2012 at 2:50

5 Answers 5

5

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

1
  • Nice trick! A bit overkill for the problem at hand though. Thanks. Nov 22, 2012 at 2:51
4

Matthew Wozniski wrote a script called vimcat.sh that does this with Vim. His version is at https://github.com/godlygeek/vim-files/blob/master/macros/vimcat.sh. I've made a few modifications to it (if memory serves, the modifications allowed it to run on my Mac OS X system; I know the substitution of /dev/fd/9 for /proc/self/fd/9 had that purpose); see my gist at https://gist.github.com/4090959.

I believe both versions of the script have trouble with returning to default background color if Vim's highlighting changes the background.

2

Like Emacs (cp. ataylor's answer), Vim can render a buffer with full syntax highlighting to HTML; see :help 2html.vim. You could probably re-use much of the plugin's code that goes through the buffer's syntax, and change it to render to ANSI escape sequences, but you'd have to re-implement all the rendering logic by yourself.

Though there are some plugins that employ Vim as a pager, I don't think it is possible to just use Vim to output the buffer with ANSI escape sequences. After all, Vim wants to retain control of the terminal, and clears it when exiting.

I'd suggest to look for another, dedicated solution outside Vim, although that means you need to install it.

1

Emacs includes a function called htmlfontify that will convert a fontified buffer to HTML. You can use this in batch mode with a small elisp script to render a file as HTML. For example:

emacs -q --batch --file myfile.rb --eval '(progn (require (quote htmlfontify)) (htmlfontify-buffer) (set-buffer-modified-p t) (save-buffer))'
1
  • Thanks. Sorry didn't make it clear, I need it in ANSI escape sequences since I'm outputting to terminal. My EmacsLisp-fu is severely limited, but you've given me some pointers to play around. Nov 16, 2012 at 1:23
1

If you don’t want to follow @IngoKarkat advice and rewrite standard plugin to support ANSI escape sequence you may use my formatvim plugin, it supports rendering to ANSI escape sequence by using

Format format csi to /path/to/file

. Initially my plugin was a rewrite of standard 2html targeting for different formats support with easy addition of a new format (I spent about 30 minutes to add ANSI escape sequence support, mostly reading specification of these sequences), but current code has gone too far to mention similarities. It is known to work faster for big files or when you use one instance of vim to render a number of files (i.e. when warmup (“compiling” and cache fill) stage is masked by the benefit of further run).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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