Bind shortcut to command palette command?

2019-03-09 04:04发布

问题:

I just installed a plugin called CodeSniffer (http://soulbroken.co.uk/code/sublimephpcs), and I want to link one of it's commands from the command palette to a keyboard shortcut because I use it so often.

Is there any easy way to do this? Or will I just need to ask the developer what the name of the command is (in the command palette it is 'PHP CodeSniffer: Clear sniffer marks')?

Thanks

回答1:

It's actually very easy to find the name of a command but it requires a few steps.

  • Open Sublime Text's built-in console (control+`)
  • Type in sublime.log_commands(True)
  • Trigger the command from the command palette

The name of the command will be logged to the console. Then open your user keybindings and create a new keybinding like this:

{ "keys": ["YOUR_SEQUENCE"], "command": "YOUR_COMMAND" }

I provided a similar answer here: Keymap Sublime Text 2 File Type?



回答2:

Another way is to crack open the .sublime-commands files.

Let's say you've installed Sublime Package Control (which you really want to do!) and then open it up in the command palette (⌘⇧p on os x) and install the Search Stack Overflow package. You'll now have two new commands in the command palette, the "Stackoverflow: Search Selection" and "Stackoverflow: Search from Input" commands.

OK, open the .sublime-commands file for the package. You need to find it first. If you're hardcore you do View > Show Console, and enter print(sublime.packages_path())

Otherwise it should be here

  • Windows: %APPDATA%\Sublime Text 2\Packages
  • OS X: ~/Library/Application Support/Sublime Text 2/Packages
  • Linux: ~/.Sublime Text 2/Packages
  • Portable Installation: Sublime Text 2/Data/Packages

and then "Search Stack Overflow/Default.sublime-commands"

This is the file that make the commands show up in the command palette in the first place.

It's another JSON file with entries like these

{
    "caption": "Stackoverflow: Search from Input",
    "command": "stackoverflow_search_from_input"
}

see, that's the command name right there: stackoverflow_search_from_input

Now just open the user key bindings JSON file and add the key binding like @BoundinCode said.