2

I have a vim script that I am sourcing with --cmd "source path/to/file.vim". The full command is a bit longer and the final command is to quit vim like so.

vim --cmd 'source path/to/file1.vim' [... source other files] --cmd ':q'

In these files I am doing some processing that outputs messages to vim with echomsg. I want to capture these messages to stdout.

The problem is some of this processing requires sleeping for a bit with sleep. However doing a sleep causes vim to redraw the screen. As a result messages that were echoed initially are partially cleared in this screen redraw.

Once I quit Vim I can see that the messages were output in sequence correctly in the shell, but the intermittent redrawing messed up the display.

One workaround I came up with is to use writefile to write the messages to a file instead of using stdout, but then I loose the ability to show progress.

Is there a better way to capture echo'ed messages to stdout from a vimscript? I would like to display the messages as they occur, as these are progress messages.

Thanks.

4

1 回答 1

1

稍后重绘可能会使消息消失,因此为避免您可能强制 a redraw,例如:

:new | redraw | echo "there is a new window"

或者lazyredraw根据评论进行检查。

要将回显(或其他消息)捕获到标准输出,您可以使用redir命令,例如:

$ ex +"redir>>/dev/stdout | echomsg 'foo' | redir END" -scq!
foo

如果您对有关文件来源的消息感兴趣,请使用-V参数,这样您的消息 ( echomsg) 也将显示,无需任何重定向,例如:

$ ex -V +"echomsg 'foo'" -scq!
...
Searching for "vimfiles/after/plugin/**/*.vim"
Searching for ".vim/after/plugin/**/*.vim" 
foo
于 2015-10-17T19:15:35.067 回答