Mike Abineri
Mike Abineri

Reputation: 399

Easier access to Sublime macros

I know how to record, save and load macros in Sublime Text 3, I also know how to bind these macros to keyboard shortcuts.

What I would like to know is there a plugin or feature in sublime that means I can have my macros showing in the sidebar ? I know that I can go to

tools->macros->user

But I was hoping that there was perhaps a package or customisation option so I can see my macros on a side panel or menu and click on them. I'm using a mac so I can't use things like auto hotkey to make a GUI with buttons.

And while I'm aware keyboard shortcuts are nice and easy, it can prove a little tricky trying remember what macro is bound to what key combination, not to mention not overwriting already existing shortcuts!

Also does anyone know what the key "name" is in Sublime for the eject button on the mac keyboard?

Upvotes: 0

Views: 355

Answers (1)

OdatNurd
OdatNurd

Reputation: 22791

What I would like to know is there a plugin or feature in sublime that means I can have my macros showing in the sidebar ?

The sidebar can only display the files and folders that you have open in Sublime; it can't display other information currently and there is also not an API that a plugin could use to add extra information to it either.

It's possible to approximate this with existing functionality, though it's a bit suboptimal. The first thing you have to do is turn off preview_on_click in your Preferences.sublime-settings so that you can click on files in the side bar without Sublime opening a preview.

import sublime
import sublime_plugin

import os


class SidebarMacroCommand(sublime_plugin.TextCommand):
    def run(self, edit, files=[]):
        macro = "res://Packages%s" % (
            files[0][len(sublime.packages_path()):]
            .replace("\\", "/")
            )
        self.view.run_command("run_macro_file", {"file": macro})

    def is_visible(self, files=[]):
        return (len(files) == 1 and
                files[0].startswith(sublime.packages_path()) and
                files[0].endswith(".sublime-macro"))

This plugin defines a sidebar_macro command that will only make itself visible in the context menu if the side bar has exactly one file selected and that file is a sublime-macro file that is somewhere inside of the Packages folder. When selected it will execute that macro.

You also need to create a file named Side Bar.sublime-menu in your User package with the following content (or add the command to the file if you already have one):

[
    { "caption": "-", "id": "end" },
    { "caption": "Run Macro", "command": "sidebar_macro", "args": {"files": []} },
]

Taken all together, if you add your User package to the side bar (or more appropriately put your macros inside of a folder in it and then add just that sub folder), you can right click on one of the displayed macros in the side bar and select Run Macro to execute it.

This will do somewhat what you want, but it does require that you use specific settings and always add a specific folder to your windows (although you could also automate that somewhat with a plugin as well).

Also does anyone know what the key "name" is in sublime for the eject button on the mac keyboard?

In order to know how Sublime sees keys, open the console with View > Show Console or the associated key (visible in the menu next to the item) and then enter sublime.log_input(True). Once you do that, every key you press that Sublime can see will be logged in the console until you restart Sublime or use False in the call to turn it off.

With logging turned on you can press any key to see what Sublime sees it as, if anything; it's possible that the operating system or some external program may see the key and use it without allowing Sublime to see it. In that case you either can't use the key or you need to find a way to make whatever it using it stop using it.

Upvotes: 2

Related Questions