Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all commands in Sublime Text 3

Tags:

sublimetext3

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?

like image 397
victrnava Avatar asked Feb 07 '18 05:02

victrnava


People also ask

Where is the command palette in Sublime Text?

To open a command palette in Sublime Text editor, you can use the shortcut key combination Ctrl+Shift+P on Windows and Cmd+Shift+P on OSX.

How do I open command prompt in Sublime Text 3?

Open any program file in the sublime editor and Right-click (context menu). You can see the Cmd menu in the Context menu options. Click on it. The command prompt will be opened through which you can compile and run your program.

How do I run commands in sublime?

Open command prompt and type sysdm. In Advanced tab, select Environment variables. Under system variables, select variable named " Path " and click Edit . Add " C:\Program Files\Sublime Text; " to the end of the existing string. Save the changes and restart command prompt.

What is command palette in Sublime Text?

The command palette is an interactive list bound to Ctrl+Shift+P whose purpose is to execute commands. The command palette is fed entries with commands files. Usually, commands that don't warrant creating a key binding of their own are good candidates for inclusion in a . sublime-commands file.


1 Answers

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
like image 51
OdatNurd Avatar answered Sep 24 '22 18:09

OdatNurd