问题
I'd like to get a list of all available commands in sublime text 3 (built-in and from packages)
What I'm trying to do:
Create shortcuts
I'm trying to create a shortcut for a package command but I don't know the name of the command. I can find the command and use it using alt + shift + p
but then when trying to add the shortcut to my .sublime-keymap
file, I'm not sure that to put on the "command": "?"
bit. I'd be great if I could just list all commands and grep for what I'm looking for then just copy paste the formal command name into the keymap file.
Explore
I'd like to explore all the commands that are available (built-in and from packages) to understand Sublime Text capabilities. Rather than searching for commands within sublime or reading tutorials online, I'd like to ask my editor:
What can you do?
rather than
Can you do this?
回答1:
There is a fairly complete list of the core commands in Sublime available via the Community Documentation, in particular in the command list section. This however doesn't help you to learn about commands that third party packages and plugins may have added, however.
In your question you mention knowing how to get at a command but not knowing what it might be for the purposes of using it elsewhere. If you're in the situation of knowing some way to invoke a command (key, command palette, menu) and wondering what the command is, Sublime has you covered.
If you open the Sublime console with Ctrl+` or View > Show Console
, you can enter the following command:
sublime.log_commands(True)
Now whenever you do anything, Sublime logs the command that it's executing the console, along with any arguments that it might take. For example, if you turn on logging and press each of the arrow keys in turn, the console will display this:
command: move {"by": "lines", "forward": false}
command: move {"by": "lines", "forward": true}
command: move {"by": "characters", "forward": false}
command: move {"by": "characters", "forward": true}
Using this facility you can figure out what commands various actions take, so that you can use them elsewhere. This is also a handy technique for diagnosing things like keyboard shortcuts that don't seem to do what you think they should do, for example. Run the same command with False
instead of True
(or restart Sublime) to turn the logging off.
If you're really interested in the gritty internal details of every possible command, something like the following is possible. This implements a command labelled list_all_commands
which, when you run it, will list all of the available commands of all types into a new scratch buffer.
Note that not all implemented commands are necessarily meant for external use; plugins sometimes define helper commands for their own use. This means that although this tells you all of the commands that exist, it doesn't mean that all of them are meant for you to play with.
Additionally, although this lists roughly the arguments that the run
method on the command class takes (which is what Sublime executes to run the command), some commands may have obscure argument lists.
import sublime
import sublime_plugin
import inspect
from sublime_plugin import application_command_classes
from sublime_plugin import window_command_classes
from sublime_plugin import text_command_classes
class ListAllCommandsCommand(sublime_plugin.WindowCommand):
def run(self):
self.view = self.window.new_file()
self.view.set_scratch(True)
self.view.set_name("Command List")
self.list_category("Application Commands", application_command_classes)
self.list_category("Window Commands", window_command_classes)
self.list_category("Text Commands", text_command_classes)
def append(self, line):
self.view.run_command("append", {"characters": line + "\n"})
def list_category(self, title, command_list):
self.append(title)
self.append(len(title)*"=")
for command in command_list:
self.append("{cmd} {args}".format(
cmd=self.get_name(command),
args=str(inspect.signature(command.run))))
self.append("")
def get_name(self, cls):
clsname = cls.__name__
name = clsname[0].lower()
last_upper = False
for c in clsname[1:]:
if c.isupper() and not last_upper:
name += '_'
name += c.lower()
else:
name += c
last_upper = c.isupper()
if name.endswith("_command"):
name = name[0:-8]
return name
来源:https://stackoverflow.com/questions/48656430/how-to-list-all-commands-in-sublime-text-3