Redirect ex command to STDOUT in vim

I am trying to create a command to reset vim highlighting information in STDOUT. I can successfully write the file as follows:

vim +'redir >outfile' +'hi' +'redir END' +'q' 

After that, the outfile contains exactly what I want. But I would prefer output to STDOUT to connect to a command that converts this information to CSS.

This approach tries to redirect the command to the register, write to the current buffer, and then send this output to tee .

 vim -E +'redir @a' +'silent! hi' +'redir END' +'put a' +'w !tee' +'q!' > outfile 

This is pretty close, but it displays the leading paging message (another 255 lines) and some ANSI removal at the end. The nomore installation nomore not suppress the message for me. I want to send the exact result sent to the outfile in the first STDOUT command.

+4
source share
3 answers

Vim supports stderr output, which we can redirect to stdout to solve your problem. However, be careful, this function is for debugging purposes only, so it has several edges. Here is a short essay on how you could do this.

First, we will run Vim in silent or batch mode, which is enabled with the -e -s ( :h -s-ex ) flags. We disabled the -n swap files because they would worry if we needed to kill Vim when it got stuck.

Instead of passing Ex commands as command line arguments, we send the script file with -s . Create a hi.vim file with the following contents:

 verbose highlight 

:verbose requires Vim to display :highlight in stderr. Let's see what we have:

 $ vim -n -e -s -S hi.vim 

Don't run it yet or you'll be stuck in the darkness of the headless Vim!

Add the command :quit and redirect stderr to stdout:

 $ vim -n -e -s -S hi.vim +quit 2>&1 

Voila! Now connect this mess to any file or utility in your heart.

There are very detailed articles on this topic, " Vim as a System Interpreter for vimscript .


Finer points: Due to the way Vim interacts with the terminal environment, it cannot write the corresponding final UNIX LF lines in batch mode. Instead, it writes line endings that look like CRLF line endings.

Consider adding a filter to get rid of them:

 $ vim -n -e -s -S hi.vim +quit 2>&1 | tr -d '\r' | my-css-util 

This answers the general question about how to "redirect the ex command to STDOUT in vim", but this does not necessarily work for your problem :hi due to this restriction caused by the -e -s flags:

'term' and $ TERM are not used.

+8
source

I came across this by mistake when running your first command. The second time I ran it, it went to stdout. I think this is because the file already exists. So you can try (by sending what already exists):

 vim +'redir >/dev/null' +'hi' +'redir END' +'q' 
+1
source

If you are working with a Unix-compatible environment, you can always use a special file, for example:

 ex +"redir>>/dev/stdout | hi | redir END" -scq! 

This will print any Vi / Ex command to standard output, which you can analyze further.

+1
source

Source: https://habr.com/ru/post/1482603/


All Articles