vimscript:在“确认”后清除命令行

ewo*_*wok 2 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
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,这最终会迫使用户[Enter]在执行命令后多打一段时间,因为命令行已扩展为显示提示和消息。

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

DJM*_*hem 7

如果你告诉 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
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样,但: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"
Run Code Online (Sandbox Code Playgroud)