问题
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?
回答1:
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
回答2:
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.
回答3:
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.
回答4:
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))'
回答5:
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).
来源:https://stackoverflow.com/questions/13408134/utilizing-emacs-or-vims-syntax-highlighter-for-command-line-program