Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vimscript: clear command line after `confirm`

Tags:

vim

I have a function that calls confirm to get user input, performs an action, then prints a message to the user:

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

The issue I'm having is that this ends up forcing the user to hit [Enter] an extra time after executing the command, because the command line has expanded to display the prompt and the message.

Is it possible to prevent this, so that after the user enters a value for the prompt, the command line is cleared, the call system is executed, then the single line displaying "Did thing!" is printed in the command line, allowing the user to immediately continue working?

like image 394
ewok Avatar asked Oct 21 '25 11:10

ewok


1 Answers

If you tell vim to refresh the screen with :redraw, this fixes your problem:

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

I don't know exactly why this works, but :redraw is mentioned in the help page for 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"
like image 164
DJMcMayhem Avatar answered Oct 24 '25 04:10

DJMcMayhem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!