Xcode Productivity Tips

Being used to IDEs like Eclipse, I feel kind of crippled when I’m using Xcode. Should I start using AppCode? Probably. But for now, some tips to make life a bit easier.


Custom Shortcuts

In Eclipse, I’m used to constantly moving code around with my keyboard. Two simple examples:

  • Alt + up/down: Move selected code up/down
  • Alt + Cmd + up/down: Copy selected code up/down
  • Cmd + d: Delete current line or selection

While using Xcode, I started to notice that not having these three shortcuts cut down my productivity significantly. To make things worse, Xcode does not have these shortcuts out of the box. Fortunately, this is not where this story ends. Let’s take a peek at this file:

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

This file contains quite some key bindings, and, as you can see, they are mappings to one or multiple messages. So, let’s add our custom section of key bindings to the end of the file:

...
  <key>My Custom Shortcuts</key>
<dict>
<key>Delete Current line</key>
<string>moveToBeginningOfLine:, deleteToEndOfLine:,
deleteToEndOfParagraph:</string>
    <key>Move Line Up</key>
<string>moveToBeginningOfLine:, selectLine:, cut:, moveUp:,
paste:, moveUp:</string>
    <key>Move Line Down</key>
<string>moveToBeginningOfLine:, selectLine:, cut:,
moveDown:, paste:, moveUp:</string>
    <key>Copy Line Up</key>
<string>moveToBeginningOfLine:, selectLine:, copy:,
moveToBeginningOfLine:, paste:, moveUp:</string>
    <key>Copy Line Down</key>
<string>moveToBeginningOfLine:, selectLine:, copy:,
moveDown:, paste:, moveUp:</string>
</dict>
</dict>
</plist>

Ok, so Xcode can be a bit flaky when you’re adding shortcuts while it’s running, so I recommend to restart Xcode before wiring up your new shortcuts. After restarting Xcode, go to the Key Bindings section in the Xcode preferences (Cmd + ,). At the bottom it should show your custom key bindings, ready to wire up!

Wire up your custom shortcuts in preferences (Cmd + ,)

Ok, although this is a step in the right direction, it does not implement the logic of Eclipse. For instance, in Eclipse, this behaviour works both on selections and on the current line if there’s no active selection, while the shortcuts above only work on the current line.


Refactor Shortcuts

In Eclipse, I constantly hit Cmd + 1 for all sort of context-aware actions. I use this mostly for refactoring: Renaming a variable, extracting code to a method, etc. In Xcode you can only bind such actions to separate keys…

Luckily, OS X comes to help here! Hit Cmd + Shift + / which will open the help menu, type ‘Refactor’ and use your up/down keys to select your desired action. It’s not as handy as Cmd + 1 in Eclipse, but hey, it’s close.

Hit Cmd + Shift + / to open the help/search feature of OS X and quick search your refactor functionality

Yes, this trick can be used in all OS X applications. I use it a lot in Eclipse too, e.g.: Cmd + Shift + / and type ‘get’ gives me ‘Generate getters and setters…’. Also, OS X let’s you define custom shortcuts for each action accessible through the menu bar.


Interface Builder Runtime Attributes

Sometimes, you create a view that should be transparent. Working with transparent views in IB can be very annoying, as you are unable to see them. A solution is to make the view visible by giving it a color:

Giving a transparent view a color

However, running your app, this view should be transparent. You could wire your view up with your code using an IBOutlet, setting the color to [UIColor clearColor], but… no… let’s not do that.

Xcode actually has a built-in trick for this! If you go to the Identity Inspector (Alt + Cmd + 3), you can add so-called User Defined Runtime Attributes. So, in our case, we add an entry for backgroundColor, making the color transparent on runtime, but being able to distinguish the view while designing in IB.

Hit Alt + Cmd + 3 and add some User Defined Runtime Attributes
Like what you read? Give Jasper Kuperus a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.