Sublime Forum

Multiple keybinding of ESCape key

#1

I have configured the ESC key to close the current tab by adding
{ “keys”: “escape”], “command”: “close” },
to my Users Key Binding file. (Windows)

I also configured Sublime 3 to close if there is no tab opened, which means I can close Sublime with ESC what is great for my workflow.

Unfortunately that also means that I can not close the search/replace bar with ESC anymore because now the tab gets closed.

Is it possible to configure the ESC key to first close the searchbar only if open but preserve the “close the tab if no searchbar is present” feature?

0 Likes

#2

Key bindings that are defined later always take precedence over previously defined bindings. Since the User package gets loaded last you will override any keybinding defined so far, as long as you don’t define a context.

The escape key is actually used in a lot of places, for closing a variety of panels (find, output, goto anywhere), transient views (which close likely takes care of), toggling command and insert mode for vi emulation, closing the auto complete popup etc…

Your only options are:

  1. Define a context for your binding that excludes all the above cases for running the close command.

  2. Copy and paste ALL escape bindings from the default package to your user bindings file and insert them AFTER your binding so they can override your binding again.

  3. If you are using ST3 you can make use of package overriding by saving a copy of the default keybindings file in Packages/Default/Default (<platform>).sublime-keymap and placing your keybinding at the beginning. (Read more: sublimetext.com/docs/3/packages.html)

  4. Use a different key.

  5. Is probably the best option.

1 Like

#3

Thank you for your helpful reply.

I have put the default config after my own keybindings in the userfile which seems to work just fine. I will switch to a better option if I get more familiar with sublime - I am just starting :smile:

0 Likes

#4

I was about to complain about the current state of affairs and then I saw that it has been discussed here before.

I am not sure I follow option 1. How can I define a context which says “if none of these other contexts is true” other than … oh I see … oooooohhh! You mean go copy all the context statements from all the existing bindings that I can find, except invert the logic of them? Like this?

    {"keys": ["escape"], "command": "sbp_quit",
        "context": [
            { "key": "sbp_has_visible_selection"},

            // inverse of all the "escape" key things found in Default plugin
            { "key": "num_selections", "operator": "equal", "operand": 1 },
            { "key": "has_next_field", "operator": "equal", "operand": false },
            { "key": "has_prev_field", "operator": "equal", "operand": false },
            { "key": "panel_visible", "operator": "equal", "operand": false },
            { "key": "overlay_visible", "operator": "equal", "operand": false },
            { "key": "auto_complete_visible", "operator": "equal", "operand": false }
        ]
    },

So basically, when things stop working, we need to periodically update our own entries.

And if we’re writing a plugin, we should put things kind of thing in the plugin as well, if we want to play nice with the existing infrastructure.

0 Likes

#5

Yes, exactly.

0 Likes