Geertjan's Blog

  • October 24, 2006

Changing Keybindings in Other Modules

Geertjan Wielenga
Product Manager
I learnt something interesting about key bindings in NetBeans today. They're defined in files called keybindings.xml. Such files are available on an all-language level, as well as on language-specific levels. If you're in the Projects window for a module project (any module project, other than a library wrapper module project, in a module suite project, if you're using a module suite project), expand "Important Files", then expand "XML Layer", and then expand "this layer in context". If you then expand "Editors", you see a whole bunch of folders, one of which is "text". Expand "text" and then you see "base" (which is applicable to all languages), as well as a folder for each specific language.

If, for example, you want to change the key bindings for the "Comment" and "Uncomment" menu items in the Source menu, you need to look in the "x-java" folder, because both those menu items are Java-specific (you can't use them to comment/uncomment anything other than lines of Java code). Look in the "Defaults" folder and there your journey finally ends... at the keybindings.xml file. (If you don't have a keybindings.xml file there, that's okay too because you can just copy and paste the file below into the language-specific folders, within a "Defaults" folder that you can also create yourself, which generates the XML entries that you see later in this blog entry.) So, this picture describes the trip I just outlined in words:

Initially, the content of the above file, i.e., the one in the x-java folder in the screenshot above, is as follows:

<?xml version="1.0"?>
<!DOCTYPE bindings PUBLIC "-//NetBeans//DTD Editor KeyBindings settings 1.0//EN"
<!-- IMPORTANT: To handle Apple-specific keybindings, there is another copy of
this file, with minor diffs, in ide/applemenu. Any changes made here should
be made there too -->
<bind actionName="comment" key="DS-T"/>
<bind actionName="fast-import" key="OS-I"/>
<bind actionName="fix-imports" key="OS-F"/>
<bind actionName="goto-declaration" key="O-G"/>
<bind actionName="goto-help" key="O-F1"/>
<bind actionName="goto-source" key="O-O"/>
<bind actionName="goto-super-implementation" key="D-B"/>
<bind actionName="javadoc-show-action" key="CS-SPACE"/>
<bind actionName="macro-debug-var" key="D-J$D"/>
<bind actionName="make-getter" key="O-U$G"/>
<bind actionName="make-is" key="O-U$I"/>
<bind actionName="make-setter" key="O-U$S"/>
<bind actionName="select-element-next" key="OS-S"/>
<bind actionName="select-element-previous" key="OS-A"/>
<bind actionName="try-catch" key="OS-W"/>
<bind actionName="uncomment" key="DS-D"/>

So, let's change the "comment" and "uncomment" key bindings to "D-B" and "SD-B", respectively. Do that, but... be careful! Notice that the action "goto-super-implementation" also uses "D-B". And it comes after "comment". Therefore, either remove that action or change that one too, to something that isn't used somewhere else.

After the change, you'll see that the folders leading up to the file become bold (handy, because now you can see at one glance where the files are that you have changed). Also notice that there's a keybindings.xml file generated in the same folder as where the layer.xml file is found. And notice that this one contains your changes. And that the layer.xml now has entries like this:

<folder name="Editors">
<folder name="text">
<folder name="x-java">
<folder name="Defaults">
<file name="keybindings.xml" url="keybindings.xml"/>

(Now ask yourself: "Did I type even one single tag in the layer.xml file?" and then ask yourself: "If NetBeans hadn't generated the tags for me, what would the chance have been of me mistyping something, like leaving out a back slash or not closing an element correctly?" And then say to yourself: "Pretty cool, NetBeans did all the work for me, and I can trust that the entries are correct and I won't need to second-guess myself.")

So now, for the specified language in the layer.xml file, the keybindings.xml file that you created (by overriding the existing keybindings) will be loaded. You will see "Ctrl-B" and "Ctrl-Shift-B" in the Source menu, for "Comment" and "Uncomment":

(Alternatively, you could add the comment and uncomment key bindings to the keybindings.xml file in text/base/Defaults. But it is better to group key bindings by the language they belong to.)

It's maybe a little bit tricky but it shows (as so often) that if you want to do something that you don't know how to do in the layer.xml file, just go to "this layer in context" and do a search. (Think of "this layer in context" as being a massive merge of ALL layer.xml files from ALL modules available in the application. Therefore, if you want to change something that comes from someone else's layer.xml file, the FIRST place to go to is "this layer in context".) I searched for "comment" and that's how I ended up in the keybindings.xml file. This question came from Ian and Crystal (see mail thread here) who I met at the NetBeans User Group in Munich. I hope this helped! If not, please leave a comment here with further questions...

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.