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?
5.43 How do I turn on syntax highlighting? font-lock-mode is the standard way to have Emacs perform syntax highlighting in the current buffer. It is enabled by default. With font-lock-mode turned on, different types of text will appear in different colors.
Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.
Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible for colorizing keywords like if or for in JavaScript differently than strings and comments and variable names.
There is no simple way to obtain syntax highlighting in GNU Bash (or GNU Readline), but it is in principle possible to implement your own line editor in Bash script by binding all the user inputs to shell functions using the builtin command bind -x 'BYTE: SHELL-COMMAND' .
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
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.
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))'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With