Sublime Forum

[Solved] How to create a menu command?

#1

There is this example http://stackoverflow.com/a/18656619/4934640

import os, sublime_plugin
class CmdCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        file_name = self.view.file_name()
        command = "cd " + file_name + " & start cmd"
        os.system(command)

I do not see any means how does the command “cmd” is found out by sublime and run the CmdCommand class method run(2).

Context.sublime-menu:
[
     { "command": "cmd" }
]

How may I create my custom command like this:

[
    {
        "caption": "Preferences",
        "mnemonic": "n",
        "id": "preferences",
        "children":
        [
            { "caption": "Force Re-write Sublime Settings", "command": "force_reload_sublime_settings" },
        ]
    }
]

I may just run sublime.save_settings('Preferences.sublime-settings') on the Sublime Text console. But how to create a plugin to accomplish so? Something like this:

import os, sublime_plugin
class CmdCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view.run_command( "sublime.save_settings('Preferences.sublime-settings')" )
0 Likes

#2
import sublime_plugin, sublime

class ForceReloadSublimeSettingsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sublime.save_settings('Preferences.sublime-settings')

ST infers the command name from the name of the TextCommand class you create, and converts camel case to lowercase with underscores, and ignores the Command suffix.

5 Likes