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.

I'm using Vintage mode (Vi bindings) on Sublime Text 2.

When switching back to ST2, I regularly get bitten by still being in insert mode, resulting in me entering "jjjjjjjjjjjjkkkkkkkkk".

Is there a way to automatically leave insert mode and go back to command mode when the ST2 window loses focus?

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

I've never used Sublime Text 2, but it looks like it might have promise... not changing from my Vim though {Grin}

The following SHOULD work for you to turn off Insert Mode when you lose focus, name it "stop_insert_on_focus_lost.py" and place it somewhere in your plugins folder.

Hopefully it'll work, had to scan the API and some of their example plugins to get the format and commands. (never seen ST2 before though... do I get points for grokking their API, Python & Vintage mode in a few minutes?)

import sublime, sublime_plugin
import os.path

class StopInsertOnFocusLost(sublime_plugin.EventListener):
    def on_deactivated(self, view):
        view.run_command('exit_insert_mode')
share|improve this answer
    
Wow, this actually works -- you are incredible. (I dropped it into Packages/StopInsertOnFocusLost/.) Thanks a lot! –  Jo Liss Jul 9 '12 at 13:33
    
Glad I could help. –  lornix Jul 9 '12 at 17:05
    
I just noticed: When adding this plugin it will cause your search and replace fields to change to normal mode, too. So you always have to press "i" to get into insert mode to insert your search term. How can we fix this? –  Sebastian Hoitz Jul 25 '12 at 17:26
    
Umm, Use VIM? Read the Sublime Macro Documentation? Not like there's a lot to it... 1 line of code, 4 lines of header. Of COURSE it's going to shift things out of insert mode... it called 'stop-insert-on-focus-lost'. Maybe you could save the 'search & replace' field current modes to be restored on window focus return? Easiest fix... don't switch to any other apps, thus not losing focus... yay! no mode changes for S&R fields. (Theoretical Question: What are 'search & replace fields'? Must be a clicky-mousy thing. {Shrug}) Enjoy! {Grin} (Why would you stay in insert mode?) –  lornix Jul 25 '12 at 20:15
1  
Example: I press [/] to open up the search bar. But now I have to press [I] first before I can insert my search term. –  Sebastian Hoitz Aug 7 '12 at 13:16
add comment

The plugin above is great but suffers from an annoying bug, as mentioned in the other comments. When you switch to something like the find menu or the go to anything popup you will be in command/normal mode there. What's worse, commands won't work entirely correctly because many of the vintage mode commands won't run in popups, so you're in a worse of both worlds situation.

This version fixes that:

import sublime, sublime_plugin
import os.path

class StopInsertOnFocusLost(sublime_plugin.EventListener):
    def on_deactivated(self, view):
        view.run_command('exit_insert_mode')

    def on_activated(self, view):
        if view.settings().get('is_widget') == True:
            view.run_command('enter_insert_mode')

Thanks to the original answer for the starting code!

share|improve this answer
add comment

I also have this to offer:

import sublime, sublime_plugin
import os.path

class StopInsertOnFocusLost(sublime_plugin.EventListener):
    def on_deactivated(self, view):
        if view.file_name() and not view.settings().get('command_mode'):
            view.run_command('exit_insert_mode')

Adapted from lornix's answer -- fixes some edge cases. Name it stop_insert_on_focus_lost.py and add it under Packages.

share|improve this answer
add comment

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.