Luis Solano

Bits on Software

Custom Xcode Shortcuts

| Comments

People call me lazy but they just don’t understand, I’m just efficient! Or that’s what I keep telling myself.

A few weeks back I decided to optimize my workflow as much as possible and, since Xcode is the application I spend most of my time in front of, I decided to start with it. After writing a bunch of Ruby using TextMate back when I was working at ShopKeep, I got really used to a few keyboard shortcuts that are missing in Xcode. I’m talking about “Delete current line” and “Duplicate current line”. Pretty self-explanatory.

I also find Vim’s “Insert line below” (o) and “Insert line above” (O) really useful too. Let’s say the cursor is in the middle of a line and you want to enter a new line below. You have to go to the end of the line and hit return or similar, but that’s just too many keystrokes. With these shortcuts you can get that in a single keystroke including the right indentation of the new line.

Let’s see how to get these shortcuts (and any other) into Xcode:

First we need to modify a plist file that lives inside the bundle (needs sudo to edit)


Add these few lines inside the section “Insertions and Indentations”

<key>Insert New Line Below</key>
<string>moveToEndOfLine:, insertNewline:</string>
<key>Insert New Line Above</key>
<string>moveUp:, moveToEndOfLine:, insertNewline:</string>
<key>Duplicate Current Line</key>
<string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>

and these inside the section “Deletions”

<key>Delete Current Line</key>
<string>selectLine:, delete:</string>

Now restart Xcode.

Open Xcode preferences and go to the “Key Bindings” tab. Use the search bar to filter and look for the new shortcuts. You can double click the desired shortcut to define a key binding. These are mine:

  • Delete current line: ctrl + shift + k
  • Duplicate current line: ctrl + shift + d
  • Insert line below: ctrl + o
  • Insert line above: ctrl + shift + o

Boom! You should be good to go.

As I mention, you can define any arbitrary shortcut using operations like “moveUp:”, “copy:”, etc. Just take a look at the plist file to see most of the available actions. The avid observer will notice that these actions look like an Objective-C selector and, in fact, they are. The actual implementation of these shortcuts relies on sending a selector defined in a plist to an object, pretty clever. For a complete list of selectors check this Xcode class dump, in particular the classes DVTTileView.h and HFTextRepresenter.h

Follow me on twitter for more tips like this