Utilizing emacs' or vim's syntax highlighter for command
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?
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[27mrn
e[An((?s).*?)
(?:e[Kn)+
e.*e[27mr$(?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
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.
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.
链接地址: http://www.djcxy.com/p/47106.html上一篇: 用于语法高亮显示的标准颜色