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.

Darshan Sawardekar
  • 5,065
  • 2
  • 21
  • 31
  • possible duplicate of [Redirect ex command to STDOUT in vim](http://stackoverflow.com/questions/16739300/redirect-ex-command-to-stdout-in-vim) – glts Aug 05 '13 at 19:03
  • Also see [Vim execute a command and send out buffer over stdout](http://stackoverflow.com/q/17266098). – glts Aug 05 '13 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. – Darshan Sawardekar Aug 05 '13 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... – Ingo Karkat Aug 05 '13 at 19:34
  • @IngoKarkat more or less. :) Is this possible? – Darshan Sawardekar Aug 05 '13 at 19:49
  • Have you checked the `'lazyredraw'` option? Maybe together with `:redraw` you can echo the messages correctly. – mMontu Aug 06 '13 at 18:01
  • `lazyredraw` sounds useful. I ended up writing to a buffer to display progress and redirecting that to a file on close. Thanks for the help. – Darshan Sawardekar Aug 06 '13 at 19:47

1 Answers1

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
kenorb
  • 155,785
  • 88
  • 678
  • 743