1

I want to write an autocmd that prompts the user when the user tries to quit Vim. Based on the user's response, Vim should either quit, or cancel the quit. Is this possible using QuitPre? If not, how can I implement such functionality using Vimscript?

Simple example:

function! s:confirm_quit()
    let choice = confirm('Really quit?', "&Yes\n&No")
    if choice == 0
        " TODO: Prompt again.
        " ...
    else if choice == 1
        " TODO: Quit.
        " ...
    else if choice == 2
        " TODO: Do not quit.
        " ...
    else
        " Invalid choice.
        " ...
    endif
endfunction

autocmd QuitPre * call s:confirm_quit()
3

Is this possible using QuitPre?

"Quit" means quitting a buffer as in :quit. You probably want ExitPre instead.

can I implement such functionality using Vimscript?

No, AFAIK, the action cannot be aborted by autocmd.

Based on the user's response, Vim should either quit, or cancel the quit

Simply :set confirm and no worries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.