Reputation: 22710
In TextMate there can be multiple commands bound to the same keystroke. When you enter that keystroke, TextMate simply shows a small menu near your cursor listing the different commands, and letting you choose one by typing a number.
I'd like to do this (or something very similar) in Sublime Text. I would greatly prefer if there's a way of getting Sublime (or a plugin for Sublime) to manage the overloading itself, so I can bind two things to the same key (or two different packages can bind different commands to the same key) and have everything just work.
It would be MUCH less ideal if I have to manually manage this, such as defining some sort of menu, and then having it offer the two commands. But if that's the only solution, I'm still interested in knowing how to do it.
Upvotes: 0
Views: 176
Reputation: 102942
While it would be possible to create a plugin that pops up a menu of actions when a certain key combination is pressed, a (potentially) better way would be to use contexts in your custom key binding. You can define multiple key binding definitions using the same key combination, but perform different actions depending on the context - for example, whether the cursor is in a certain scope, or a certain regex matches or doesn't match surrounding text, or whether a certain setting is one value or another, etc. While this does require a certain amount of setup ahead of time, contexts are extremely powerful.
Upvotes: 1
Reputation: 48
In Sublime Text 3 (not sure about 2), whichever key binding + command combination you enter last is considered and run. Also under the Preferences Menu there are 2 options for Key Bindings - Default and User. The key binding + command combinations in the User file will always override those in the Default file.
To answer your question, I just tried and it's possible. What you'd have to do, though, is to make a Plugin in Sublime Text, assign a key binding to it in Key Binding - User and then in your plugin you can run whatever commands you wish to. If they are Sublime Text predefined commands then you can include them as shown in the code below. I've overloaded the "Ctrl + Tab" keybinding here.
#Key Bindings - User file (sublime-text-3/Packages/User/Default (Linux).sublime-keymap )
[
{ "keys": ["ctrl+tab"], "command": "test_overload_two" }
]
I created a new file under (sublime-text-3/Packages/User ) All files created here are loaded as plugins in ST3. Make sure the file has a .py extension (python)
#Overload.py
import sublime, sublime_plugin
class TestOverloadTwoCommand(sublime_plugin.WindowCommand):
def run(self):
sublime.message_dialog("Command 2")
self.window.run_command("next_view_in_stack")
So what happens when I press the Ctrl + Tab key binding is first a Sublime Message Dialog shows up with "Command 2" and then when i click "ok" on the Dialog Box, the view shifts to the next view(tab) open in Sublime.
So that's what you can do. Hope this answers your question.
Upvotes: 0