The website has moved

Note: the website moved to www.keenformatics.com. You're being redirected.

Thursday, October 20, 2016

How To Override Sublime Text Packages Shortcuts and Preferences

I recently began using a Sublime Text 3 package that automatically generates inline YARD documentation for my Ruby code (Yardgen). The only problem with this package is that the key bindings it provides are overriding other Sublime Text shortcuts that I want to keep.

Since this plugin is packed in a zip file, it is not possible to simply edit one of its keymap files (see Package Control - Customizing Packages). At the same time, unpacking the original zipped file and creating a new zip file would not work.

Following are the steps that you need in order to solve this problem and, more generally, to override Sublime Text 3 packed packages preferences.

NOTE: If your package is overriding some default binding you wanted to keep, but you do not know the command it was binded to, please refer to my previous post.

The steps!

Install your package (Yardgen, in my case) using the Package Manager or any other method you prefer.

Your zipped package file should be now placed within the folder ~/.config/sublime-text-3/Installed Packages/<your-package>.sublime-package.

Check the content of the package by unzipping it (just make sure to keep the original zipped file).

In my case I want to edit one of the default Yardgen keybindings. Key bindings for Linux are usually stored in the Default (Linux).sublime-keymap file within the Yardgen package archive. This is the file content:

[
  { "keys": ["ctrl+enter"], "command": "yard_gen"},
]

We now have to create a folder named Yardgen inside ~/.config/sublime-text-3/Packages. We can then place inside it our new key bindings file that will override the default package behaviour:

cd ~/.config/sublime-text-3/Packages
mkdir Yardgen
gedit "Default (Linux).sublime-keymap"

Now copy the following json content in Default (Linux).sublime-keymap:

[
  { "keys": ["ctrl+alt+shift+enter"], "command": "yard_gen"},
]

As you can see, we just associated the yard_gen command to a different key sequence. You can obviously choose the one that best suits your needs.

At the moment, the yard_gen command is binded to two different key sequences: the default one, still present in the original package, and the new one we just defined. In my case, I’m not ok with this. Yardgen key bindings have in fact overridden the default CTRL + Enter behaviour, and I want to have it back.

If you too want to restore the default binding, you just need to figure out which command it was originally associated with and then explicitly add its binding to the Key Bindings preference file. For more details, please refer to my other post: How To Find out Sublime Text Key Binding Commands.


References

Tuesday, October 18, 2016

How To Find out Sublime Text Key Binding Commands

I just happened to download an awesome package for Sublime Text. Unluckily, this package uses a keyboard shortcut that overwrites a default binding I really need (the CTRL + Enter command that adds a line below the current line). I therefore want to restore the normal ST behaviour and, in order to do that, I need to figure out which command was originally called when using that shortcut.

If a new package is overwriting some default binding you want to keep, but you do not know the command it was binded to, please keep reading.

NOTE: In order to make these steps work, you need to temporarily uninstall the package that is currently overwriting your key binding.


The Steps


Open Sublime Text console. To do so, you can either click on View > Show Console or use the CTRL + ` shortcut.

We can now activate the console log to show every command we run. Give the following command in the Sublime Text console:

sublime.log_commands(True)

Now press (in the appropriate context) the key sequence that you want to analyze. For example, if you want to find out the command associated with CTRL + Enter, press that sequence of keys while editing a file. In this case you will see something like the following appear in your console:

command: run_macro_file {"file": "res://Packages/Default/Add Line.sublime-macro"}

That is the command we are looking for. We can now deactivate the command logging feature before we proceed:

sublime.log_commands(False)

If you need to overwrite some package key bindings and restore the old ones, you can simply bind your old key sequence to that command. In the CTRL + Enter case, add this entry to your Default (Linux).sublime-keymap json file (Preferences > Key Bindings):

{ "keys": ["ctrl+enter"],
  "command": "run_macro_file",
  "args": {
    "file": "res://Packages/Default/Add Line.sublime-macro"
  }
}

If you also want to keep your new package shortcuts, binding them to a different key binding, please refer to How To Override Sublime Text Packages Shortcuts and Preferences.


References