Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there any way to leave insert mode automatically once MacVim is put in background, so when I came back it's in normal mode?

Often I change the foremost application an once I come back to MacVim I see myself inserting lots of 'j's and 'k's. It would be best practice to get more habituated to press , but it would save some strokes specially when working on a website which needs a lot of edit/refresh.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

to quote http://stackoverflow.com/questions/2968548/vim-return-to-command-mode-when-focus-is-lost:

The following autocommand would be the "obvious" choice.

au FocusLost,TabLeave * stopinsert

Unfortunately, it only seems to be working properly for TabLeave. The FocusLost event is triggering but for some reason the stopinsert command isn't actually taking effect until after a key event is received once Vim has regained focus.

Instead, you can take advantage of feedkeys and the "Get me to normal mode no matter what!" key combo:

au FocusLost,TabLeave * call feedkeys("\<C-\>\<C-n>")

The only downside is that feedkeys() requires at least Vim 7. This shouldn't be a big deal, though, since Vim7 was released back in 2006.

share|improve this answer
    
Thanks akira. Is this a StackOverflow question? –  sidyll Jan 22 '11 at 18:10
    
@sidyll: at least i dont think so. it fits more here than over there. –  akira Jan 22 '11 at 18:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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