A
A
Alexander Taratin2015-01-07 11:45:28
Keyboard
Alexander Taratin, 2015-01-07 11:45:28

Sublime. How to set up hotkeys?

How to configure hotkeys in sublime text 3 so that commands from different plugins are executed depending on the extension of the open
file

[
  { "keys": ["ctrl+d"], "command": "htmlprettify" }
]

How can I add here
[
    { "keys": ["ctrl+d"], "command": "css_comb" }
]

only for css files?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2015-01-07
@Taraflex

I decided by finalizing one small plugin

import sublime
import sublime_plugin


class RunMultipleCommand(sublime_plugin.TextCommand):

    def exec_command(self, command, syntax):
        if not 'command' in command:
            raise Exception('No command name provided.')

        if 'syntax' in command:
            for s in command['syntax']:
                if syntax.find(s) > -1:
                    break
            else:
                return False

        args = None
        if 'args' in command:
            args = command['args']

        # default context is the view since it's easiest to get the other contexts
        # from the view
        context = self.view
        if 'context' in command:
            context_name = command['context']
            if context_name == 'window':
                context = context.window()
            elif context_name == 'app':
                context = sublime
            elif context_name == 'text':
                pass
            else:
                raise Exception('Invalid command context "'+context_name+'".')

        # skip args if not needed
        if args is None:
            context.run_command(command['command'])
        else:
            context.run_command(command['command'], args)
        return True

    def run(self, edit, commands=None):
        if commands is None:
            return  # not an error
        syntax = self.view.settings().get('syntax').split('/')[-1].lower()
        for command in commands:
            if self.exec_command(command, syntax) and command['end']:
                return

Added the ability to break the chain of commands using end:true
[
{
    "keys": ["ctrl+d"],
    "command": "run_multiple",
    "args":
    {
        "commands": [
        {
            "syntax": ["python"],
            "command": "auto_pep8",
            "args":
            {
                "preview": false
            },
            "end": true
        },
        {
            "syntax": ["css", "scss", "less", "sass"],
            "command": "css_comb",
            "end": true
        },
        {
            "syntax": ["php"],
            "command": "code_formatter",
            "end": true
        },
        {
            "syntax": ["html", "xml", "json"],
            "command": "htmlprettify",
            "end": true
        },
        {
            "syntax": ["javascript", "typescript"],
            "command": "typescript_format_document",
            "end": true
        }]
    }
},
{
    "keys": ["ctrl+shift+c"],
    "command": "color_highlighter_pick_color",
    "context": [
    {
        "key": "color_highlighter.color_highlighter_pick_color"
    }]
}]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question