VIM Smart quit macro

这一生的挚爱 提交于 2019-12-06 04:23:01
Jamie Schembri

Referring to pandubear's answer, I wrote a quirky little function which seems to do what you want.

function! QuitF3()
  try
    quit
  catch /E37:/
    " Unwritten changes.
    echo "E37: No write since last change. Press F3 again to really quit."
    if getchar() == "\<F3>"
      quit!
    else
      " Close prompt.
      call feedkeys('\<ESC>')
    endif
  endtry
endfunction

map <F3> :call QuitF3()<CR><CR>

I think you could map F3 to a function that does this sort of thing: (pseudocode)

try:
    quit
catch error 37:
    intercept next keystroke
    if it's F3:
        force quit
    else:
        send key back to vim

I don't know all the details, but I'm pretty sure you can do all of those things with :try, :catch, getchar() (iffy on this one, not sure if it'll work for special keys like F3) and feedkeys(). See the :help for each of those things.

Seems a little bit much, though. Why not make a separate mapping for :q!?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!