Sublime Forum

Add new commands

#1

This is a super basic newbie question, but I have been looking around for about an hour and I still can’t figure out how to do it. The question is this: how do you add new commands? Or to put it another way, how do you connect menu items and key bindings with python commands that you have written?

For example, I was trying to find out how the toggle_comment command referred to in Main.sublime-menu connects to the ToggleCommentCommand in comment.py. I made my own file named test.py and renamed ToggleCommentCommand to ToggleCommentTestCommand. I added a menu item that was an exact copy of the Comment menu item, but I changed the names to match the names in my test.py file:

[code] {
“caption”: “Test”,
“id”: “test”,
“children”:

                { "command": "toggle_comment_test", "args": {"block": false}, "caption": "Toggle Comment" },
                { "command": "toggle_comment_test", "args": {"block": true}, "caption": "Toggle Block Comment" }
            ]
        },[/code]

The menu items get added but they are grayed out. If I use the “toggle_comment” command instead, they work.

I can’t figure out how ToggleCommentCommand gets connected up to toggle_comment. Could someone explain it?

0 Likes

#2

For anyone reading this, I think I’ve figured it out. I’m still not sure why what I was trying wasn’t working, but when you make a python class inherited from one of the commands, you can refer to it from keymaps and menus via the underscore-separated version of the name. For example:

Python class: SomeTestCommand
In keymap: some_test

0 Likes