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.

7
  • possible duplicate of Redirect ex command to STDOUT in vim Commented Aug 5, 2013 at 19:03
  • Also see Vim execute a command and send out buffer over stdout. Commented Aug 5, 2013 at 19:04
  • Both these are good answers, but don't solve the issue. For 1. I can't use -s, because of the vimrc suppression. 2. Piping won't work as it runs after vim exits. I want the screen to update as the messages are sent, to show progress. The main issue is sleep triggers redraws which clears out the echo messages sent before. Commented Aug 5, 2013 at 19:27
  • So you want to run Vim automated inside a batch job, but still show output while it's running? That half-interactive mode is quite far from the usual use cases, no wonder it works poorly... Commented Aug 5, 2013 at 19:34
  • @IngoKarkat more or less. :) Is this possible? Commented Aug 5, 2013 at 19:49

1 Answer 1

1

A later redraw may make the message disappear, so to avoid that you may force a redraw, e.g.:

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

Or check lazyredraw instead as per comments.

To capture echo (or other messages) to the standard output, you can use redir command, e.g.:

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

If you're interested in messages about which files are sourced, use -V parameter, so your message (echomsg) would be displayed as well without any need for redirect, e.g.:

$ ex -V +"echomsg 'foo'" -scq! ... Searching for "vimfiles/after/plugin/**/*.vim" Searching for ".vim/after/plugin/**/*.vim" foo 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.