Vim supports outputting to stderr, which we can redirect to stdout to solve your problem. Careful though, this feature is intended only for debugging purposes, so it has a few edges. Here's a short sketch of how you could do this.
First, we will be running Vim in silent or batch mode, which is enabled with the -e
-s
flags (:h -s-ex
). We disable swap files -n
because they will be a bother if we need to kill Vim when it gets stuck.
Instead of passing Ex commands as command line arguments we source a script file with -S
. Create a file hi.vim
with the following contents:
verbose highlight
:verbose
is necessary to make Vim output the :highlight
message to stderr. Let's see what we have so far:
$ vim -n -e -s -S hi.vim
Don't run this yet or you'll get stuck in the darkness of a headless Vim!
Add a :quit
command and redirect stderr to stdout:
$ vim -n -e -s -S hi.vim +quit 2>&1
Voilà! Now pipe this mess into any file or utility at your heart's desire.
There's a very thorough wiki article on this topic, "Vim as a system interpreter for vimscript".
Finer points: Due to how Vim interacts with the terminal environment it can't write proper unix LF line endings in batch mode output. Instead it writes line endings which look like CRLF line endings.
Consider adding a filter to get rid of those:
$ vim -n -e -s -S hi.vim +quit 2>&1 | tr -d '\r' | my-css-util
This answers the general question of how to "Redirect ex command to STDOUT in vim", but it doesn't necessarily work for your :hi
problem, because of this constraint caused by the -e -s
flags:
'term' and $TERM are not used.
vim +'redir! >outfile' +'silent! hi' +'redir END' +'q'&&cat outfile
– Cubeb:TOhtml
? – Penelopa