Adding Custom Menus in Sublime Text

前端 未结 3 1037
广开言路
广开言路 2021-02-04 07:50

How to add a custom Menu item in SublimeText 2 .

Any Ideas ??
I see there is a Main.sublime-menu file but dont know how to edit i

相关标签:
3条回答
  • 2021-02-04 08:30

    I know this way too late to join the party and add my 2 cents. Anyway, Main.sublime-menu is a file that allows you to add menu items to the top menu i.e [File, Edit, Selection, Find, View, Goto, etc.]

    I recently added a new section "Dev" just to figure it out. I also wanted a way to trigger browser previews for a specific browser. Check it out.

      [
        {
        "caption": "Dev",
        "mnemonic": "Z",
        "id": "dev",
        "children": [
          {
            "caption" : "Previews",
            "children": [
              { "caption": "Markdown Live Preview", "command": "new_markdown_live_preview", "id": "markdown_live_preview" },
              { "caption": "Preview in Default Browser", "command": "view_in_browser", "id": "markdown_live_preview" },
              { "caption": "Preview in Firefox", "command": "view_in_browser", "args": { "browser": "firefox" }, "id": "markdown_live_preview" },
              { "caption": "Preview in Chrome","command": "view_in_browser", "args": { "browser": "chrome" }, "id": "markdown_live_preview" },
              { "caption": "Preview in Safari", "command": "view_in_browser", "args": { "browser": "safari" }, "id": "markdown_live_preview" },
              ]
            },
          ]
        }
      ]
    

    Anyway, this still works in ST3. Just in case anyone stumbles around here.

    0 讨论(0)
  • 2021-02-04 08:32

    The *.sublime-menu file is simply JSON. You can create a Main.sublime-menu in your user directory and it will be merged with other menu entries. It may be beneficial to look through the Main.sublime-menu files third party plugins have. These are generally much shorter, so may be easier to understand some of the things you need to define in each entry.

    edit

    You can use the following as a plugin to open notepad with an arbitrary file.

    import sublime
    import sublime_plugin
    import subprocess
    import threading
    class OpenNotepadCommand(sublime_plugin.TextCommand):
        def run(self, edit, filename=None):
            th = NotepadThread(filename)
            th.start()
    
    class NotepadThread(threading.Thread):
        def __init__(self, filename=None):
            self.filename = filename
            threading.Thread.__init__(self)
    
        def run(self):
            if self.filename is not None:
                subprocess.call("notepad.exe %s" % self.filename)
            else:
                subprocess.call("notepad.exe")
    

    When you are creating a menu item use something like the following for the command and arguments.

    {
        "command": "open_notepad",
        "args": { "filename": "<the absolute path here>"}
    }
    
    0 讨论(0)
  • 2021-02-04 08:46

    Easier option if what you want is just run a command. Create a file Context.sublime-menu inside your Packages/User directory, and add the following:

    [
        { "caption": "<Your caption here>", "command": "exec", "args": {"cmd": ["<your cmd name>", "<arg1>", "<arg2>", <...>]} }
    ]
    

    Exemple: Adding a menu item to the context menu that just run dir:

    [
       { "caption": "List files in current dir", "command": "exec", "args": {"cmd": ["dir"]} }
    ]
    
    0 讨论(0)
提交回复
热议问题