• About
grant davis' blog

Adding Custom Key Commands in Xcode 4

July 26, 2012 6:17 pm / Leave a Comment / grant

One of my favorite IDEs to program in, even if it feels clunky and rough around the edges, is Eclipse. I used it for years and became accustomed it its handy built-in key actions such as Duplicate Line, Delete Current Line, Move Line, etc.

Once I moved into Xcode and began working primarily as an iOS developer I quickly became annoyed at the limitations of the built-in key strokes. I’ve since discovered a way to add your own custom actions into Xcode which essentially duplicates the functionality of Eclipse, which is awesome!

To do this, you want to start by creating some actions that we’ll add to Xcode’s default key commands. Xcode uses plist filled with dictionary objects that represent sets of commands. We’re going to create our own group of commands and add it to the default set of Xcode key commands.

Here’s what my commands look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<key>GDI Commands</key>
<dict>
    <key>GDI Duplicate Current Line</key>
    <string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>
    <key>GDI Delete Current Line</key>
    <string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteBackward:, moveDown:, moveToEndOfLine:</string>
    <key>GDI Move Current Line Up</key>
    <string>selectLine:, cut:, moveUp:, moveToBeginningOfLine:, insertNewLine:, paste:, moveBackward:</string>
    <key>GDI Move Current Line Down</key>
    <string>selectLine:, cut:, moveDown:, moveToBeginningOfLine:, insertNewLine:, paste:, moveBackward:</string>
    <key>GDI Insert Line Above</key>
    <string>moveUp:, moveToEndOfLine:, insertNewline:</string>
    <key>GDI Insert Line Below</key>
    <string>moveToEndOfLine:, insertNewline:</string>
</dict>

Once you’re happy with your commands, locate the plist file we’re going to modify:

1
/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/IDETextKeyBindingSet.plist

Open this in a text editor and add the xmlandelements we created about to the end of the file, but before the finaltags.

Your file should look similar to this when finished:

 

Save the file and then open up Xcode. If you already had it open, you’ll need to restart. Go into Preferences, Key Bindings.

Here make a new set and search for your commands. I prefixed my commands with GDI so I can do a quick search and see all my custom commands.

Now, just double click the Key column for each command, add your keystroke, and start using your custom commands!

Posted in: Tutorials, Xcode

Leave a Reply Cancel reply

You must be logged in to post a comment.

Post Navigation

 
Next Post →

Archives

  • January 2014
  • July 2012

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© Copyright 2025 - grant davis' blog
Infinity Theme by DesignCoral / WordPress