vimscript:`confirm`后清除命令行

时间:2017-07-03 18:46:34

标签: vim

我有一个函数调用confirm来获取用户输入,执行操作,然后向用户输出消息:

function! PerformAction()
  let answer = confirm('Do thing?', "&Yes\n&No", 1)
  if answer == 1
    call system("do_thing")
    echo "Did thing!"
  endif
endfunction

我遇到的问题是,这最终会导致用户在执行命令后再次点击[Enter],因为命令行已扩展为显示提示和消息。

是否可以防止这种情况,以便在用户输入提示值后,清除命令行,执行call system,然后打印显示"Did thing!"的单行。命令行,允许用户立即继续工作?

1 个答案:

答案 0 :(得分:3)

如果您告诉vim使用:redraw刷新屏幕,则会解决您的问题:

function! PerformAction()
  let answer = confirm('Do thing?', "&Yes\n&No", 1)
  if answer == 1
    call system("do_thing")
    redraw
    echo "Did thing!"
  endif
endfunction

我不确切知道为什么会这样,但:redraw的帮助页面中提及了echo

                            *:echo-redraw*
            A later redraw may make the message disappear again.
            And since Vim mostly postpones redrawing until it's
            finished with a sequence of commands this happens
            quite often.  To avoid that a command from before the
            ":echo" causes a redraw afterwards (redraws are often
            postponed until you type something), force a redraw
            with the |:redraw| command.  Example: >
        :new | redraw | echo "there is a new window"