Sublime Forum

Keybinding - Purgatory

#1

There is something I cannot currently fix. I like to use different plugins, and it is a terrible idea that each plugin suggests me to use some particular keybindings. The hell starts at

  • beautify latex is Ctrl+Alt+L
  • beautify C++ is Ctrl+Alt+A
  • beautify Ruby is Ctrl+Alt+K

Of course, I can add some new ones, but disabling is currently not straight-forward. At least I don’t feel obligable to inspect and book-keep all new plugins whether they pour their keybindings in my config.

There are 3 Questions:

  1. Is there any way to set Ctrl+Alt+A as my default and let sublime decide on the context which commands to execute. I played around with several versions of

    { “keys”: [“ctrl+alt+a”],
    “command”: “beautify_command_for_cpp”,
    “context”: [
    { “key”: “selector”, “operator”: “equal”, “operand”: “source.c++” }
    ]
    },
    { “keys”: [“ctrl+alt+a”],
    “command”: “beautify_command_for_latex”,
    “context”: [
    { “key”: “selector”, “operator”: “equal”, “operand”: “source.latex” }
    ]
    },

without any success.

  1. can I some use 1. to be executed when saving the file?

  2. Plugins are currently telling me, which keybindings I am supposed to use. There are conflicts with my custom keybindings. Using “Preferences/Package Settings / / Keybindings-Default”

I can literally see these keybindings I do not want or at least not at that particular keystroke combination. But I cannot edit this file. And it would be tedious to extract each package and edit them (Good luck with updating them). There should be an option to disable all default keybindings from a package.

How do you deal with these issues?

edit: the software discourse obviously discovered a new of counting 1., 1., 2., I cannot change it, but I promise I can count.

1 Like

#2

Question 1

Syntax specific key bindings work. You are on the right track. You just need to add "match_all": true to your context.

Btw: All key bindings related to certain syntaxes should be bound to such contexts.


	{ "keys": ["ctrl+alt+a"],
		"command": "beautify_command_for_cpp",
		"context": [
			{ "key": "selector", "operator": "equal", "operand": "source.c++", "match_all": true }
		]
	},
	{ "keys": ["ctrl+alt+a"],
		"command": "beautify_command_for_latex",
		"context": [
			{ "key": "selector", "operator": "equal", "operand": "source.latex", "match_all": true },
		]
	},

Question 2

There are two options.

a) Write a plugin, with an EventListener which listens for on_pre_save to run the beautify_... command.

import sublime
import sublime_plugin

class EventListener(sublime_plugin.EventListener):

    def on_pre_save(self, view: sublime.View):
        if view.match_selector('source.c++'):
            view.run_command('beautify_command_for_cpp')
        elif view.match_selector('source.latex'):
            view.run_command('beautify_command_for_latex')

b) Write syntax specific key bindings for ctrl+s and call a macro which executes beautify_... and save. If you don’t like macros the Chain of Command package might be a good alternative.

Example using Chain of Commands:

{
    "keys": ["ctrl+s"], 
    "command": "chain", 
    "args": {
        "commands": [
            ["beautify_command_for_latex"],
            ["save"]
        ]
    },
    "context": [
        { "key": "selector", "operator": "equal", "operand": "source.latex", "match_all": true },
    ]
}

Question 3

You can’t disable key bindings but you can overwrite existing ones.

Key bindings in User/Default.sublime-keymap always overwrite ST’s default ones or those from packages. This way you can define your favorite bindings for just everything. If you still need plugin functionality bound to those rebound keys, you’ll “just” need to remap them as well in your user file. This way you don’t need to be concerned about plugin updates. Nevertheless redefining many key bindings of a couple of plugins might be a sophisticated task.

1 Like

#3

Thanks for all the nice answer. Solution 1 works great!

0 Likes