How to change keybindings?

Continuing the discussion from Home and End Key Behaviour:

FoldingText has two kinds of keybindings

  1. The standard “OS X menu item” shortcuts. These can be changed in System Preferences as described here.

  2. Internally defined keybindings. These take precedent over the “OS X menu item” shortcuts, and can be changed with a plugin.

To change an internal keybinding you need to create a plugin that adds the new binding. For example lets say you want to rebind the Home and End keys to move to the start and end of the current line instead of to scroll to the top or bottom of your document, which is the default behavior. To do this you will:

  1. Create a plugin. I’ve created this example plugin that shows in particular how to add new keybindings.

  2. Modify that plugin to override the bindings you are interested in. In this case (changing Home and End behavior) the plugin code should be modified to this:

     editor.addKeyMap({
     	'Home' : 'moveToBeginningOfLine',
     	'End' : 'moveToEndOfLine'
     });
    

    Modifiers can be combined with the bound keys with:

     Shift-, Cmd-, Ctrl-, and Alt- (in that order)
    
  3. Then install the plugin if you haven’t already done so. And create a new document, the bindings should be active in the new document.

Exploring FoldingText’s internal keybindings

If you are modifying keybindings it can be very useful to view FoldingText’s default keybindings so you know what you are overriding. It’s possible, but you’ll need to do some digging.

FoldingText ships with most of it’s own source code embedded inside the application bundle (not open source, but shared source so you can learn from it). Anyway to see FoldingText’s existing keybindings:

  1. Right click on the FoldingText.app bundle and choose “Show Package Contents”.

  2. Then browse into that folder and find the keybindings file at FoldingText.app/Contents/Resources/dist/sdk/sources/ft/editor/keybindings.js.

  3. Open that file to see FoldingText’s default text editing bindings. (There are a few others spread out through the code (you can search for addKeyMap), but that’s the vast majority of them.