BBEdit

In order to code quickly I’m used to the ability to hit ⌘↩ to move the cursor to the end of the current line and then create a new line. Also for files indentified as python source code ⇧⌘↩ inserts a : at the end of the current line and creates a new line.

First, I tried to use Clippings. Clippings do not allow you to manipulate cursor movement relative to the current line.

Next, I tried using DefaultKeyBindings.dict to achieve this.

DefaultKeyBindings.dict

{
  "@\U000D" = ( moveToEndOfLine:, insertNewline: );
  "~\U000D" = ( moveToEndOfLine:, insertNewline: );
}

Unfortunately it seems that BBEdit doesn’t use the Cocoa Text Object and therefore doesn’t use these new key mappings.

I broke down and emailed support@barebones.com about my predicament and asking for a pointer in the right direction. The reply I received point me to AppleScript.

AppleScript

I was hoping this would be as simple as the DefaultKeyBindings.dict syntax in that I could just say move the cursor to the end of the current line and then insert a new line. Not so simple. Here’s the code to actually get this to work using BBEdit’s AppleScript Dictionary:

tell application "BBEdit" to tell front window
  set lineNum to startLine of selection
  set len to length of line lineNum
  if len > 0 then
    select insertion point after (character (len) of line lineNum)
  end if
  set selection to return
  select insertion point after selection
end tell

If I want to insert a colon at the end of the line then line 7 above looks like:

set selection to ":" & return

This can also be achieved using the System Events application using the following code:

tell application "BBEdit" to tell front window
  activate
  tell application "System Events" to keystroke (key code 124 using command down)
  tell application "System Events" to keystroke ":"
  tell application "System Events" to keystroke return
end tell

Now that I have scripts that can replicate the behavior I need I can create Application Keyboard Shortcuts that trigger the scripts and I’m all set.

Issues

So apparently there are multiple ways of setting a keyboard shortcut for BBEdit. The standard method involves using the System’s Keyboard Preference Pane to map a menu item to a keyboard combo. This method does not allow one to use the Return key in the combination. The next method is from within BBEdit’s preferences itself. Again this method does not allow the use of the Return key in the combination. Finally there is a palette for some of the menu items that allows one to assign a keyboard shortcut to menu items. This method allows the use of the Return key in the keyboard combination.