Killer Xcode Tips and Tricks – Tips 1 to 10

Fri, Oct 16

My world is surrounded by sticky notes, both electronic and paper, doing my best to keep track of Xcode shortcuts, tips and tricks. I strive to use the mouse as little as possible when coding, and without these tips I’d be lost. What better place to keep track of this stuff than to write about them here.

This is the first post of what I hope is many where we can share tips and tricks on working with Xcode.

Tip #1 – Split Editor View Vertically

If you like to have multiple code windows open at once, the Split Editor option is your friend (see the little square grid icon shown below).

By default, the windows are split horizontally. If you prefer to view your code side-by-side (nice for comparing), here’s how to tell the split to be vertical

  1. Hold down Option key when clicking the Split Window icon
Tip #2 – Comment Out a Block of Code

You can quickly comment out a block of code as follows:

  1. Select one or more lines of code to comment
  2. Command-/

To uncomment a block of code, repeat the steps above.

Tip #3 – Toggle Between .h and .m Files (aka Switch to Counterpart)

Within your current code window, you can swap between the .h and .m file as follows:

  1. Command-Option Up-Arrow
Tip #4 – Map Keys to Actions (Key Bindings)

The Switch to Counterpart tip above is a real time saver. However, I often find that if I map logical key-strokes to actions I’m much more likely to get into the habit of using them. For example, I mapped the Switch to Counterpart to Option-S, as in Swap or Switch.

Here’s how to set key bindings in Xcode

  1. From the Xcode menu choose Prefereces
  2. Select Key Bindings
  3. Click on one of the Actions in the list
  4. Tap on the Keys column on the right
  5. Enter the keystroke you want to map to the action
  6. Click Ok to save your changes
Tip #5 – Jump to API Documentation

Showing relevant API documentation for anything within the SDK(s) is as simple as:

  1. Option Double-Click on relevant code

For example, in the screenshot below, after Option double-clicking on UIToolbar, a popup window shows a summary of the class information.

Tip #6 – Traverse File History

As you open and edit various files, Xcode keeps a history list of your actions, not unlike when using a web-browser. You can move through the list using the directional arrows as shown below:

The keystoke equivalent for the above is:

  1. Option-Command Left-Arrow goto to previous file
  2. Option-Command Right-Arrow goto to next file
Tip #7 – Set a Bookmark

I can’t imagine coding without having the option to set a bookmark as a placeholder for what I’m working on. I do this regularly when I need to segue to another file to find a snippet or otherwise poke around outside the current file I’m working on.

Setting a bookmark is as simple as:

  1. Control-D
  2. Enter a name for the bookmark
Tip #8 – Jump to Bookmark

There are two options to jump to a bookmark. First, you can select the bookmark icon in the upper right corner of the Editor window.

You can bring up the same menu as shown in the above figure using this keystoke:

  1. Control-4
Tip #9 – Indent / Un-indent Code

You can indent a line of code or a selected block of code as follows:

  1. Command-[ move code left
  2. Command-] move code right

The above works regardless of where you are in a line of code, in other words, you don’t have to be at the beginning of the line.

Tip #10 – Zoom Editor

You can toggle between Detail view and Editor view by clicking the Editor button as shown here:

The keystroke equivalent of the above is:

  1. Shift-Command-E
Have an Xcode Tip to Share?

If you have a favorite tip or trick, please post a comment or send tips to me here. Killer Xcode Tips and Tricks – Tips 11 to 20 will be posted next week, and suggestions are welcome!

17 comments

Open files quickly: cmd + option + D

by James on Oct 16, 2009. #

#3 alternative – from a laptop – three finger swipe up will take you from .h -> .m -> .h etc.

three finger swipe down will take you from the declaration/implementation of the method the cursor is currently in.

#6 alternative – Three finger swipe left and three finger swipe right will move you backwards/forwards through your editor history.

by Abizer Nasir on Oct 16, 2009. #

This three finger swiping support is lost in Lion, as the operating system grabs it for its own purposes. Two-finger swiping left and right now go through history, and there appears to be no gesture for switching between counterparts (which was totally cool, sad to see it go)

by Sam on Jan 23, 2012. #

control-command-UP arrow and down arrow do the counterpart switching- yay!

by Marc on Jun 19, 2012. #

Whow!!!!! Thank you! 8 out of 10 i did not know!
Absolutely awesome post!

by Sebastian on Oct 16, 2009. #

1. Indent selection: Ctrl+I
I use this constantly to re-indent code I’m editing. I never use the manual-indent keystrokes anymore.

2. Pop Symbols PopUp: Command+Option+2
By default this may have a different shortcut, maybe Ctrl+2. Pop-up the symbols then begin typing the method name you’re looking for. It’s a quick way to find a method in the current file.

3. Go to declaration/definition: Command+Double Click.
For example, double-clicking “NSString” takes you the NSString header.

4. Close All: Command+Option+W
This closes all windows of a similar type. Useful in the “Condensed” and “Default” layouts. For example, Option+Close on a source code window closes all source code windows. Option+Close on a debugger window closes all debugger windows.

5. Switch to Symbolic Counterpart: Command+Option+Shift+Up
This doesn’t have a keyboard shortcut by default, but it can be very handy.

When the cursor is in a method or function definition, this takes you to that method’s declaration, and vice versa, even if the declaration and definition are in the same file. E.g., for private interfaces declared in a .m file.

6. Compile: Command+K
Maybe obvious, but worth mentioning. This compiles only the current source file. It’s a quick way to check for syntax errors without doing a full build.

7. Next/Prev Build Warning: Command+=, Command+Shift+=
After a build, use this to iterate over any warnings and errors.

8. Strip Trailing Spaces (Scripts!)
I bet most people don’t realize how easy it is to create custom scripts in XCode.

Try this:
- Script Menu > Edit User Scripts…
- New Shell Script
- Input: Entire Document
- Output: Replace Document Contents
- Script:

#!/usr/bin/perl

while () {
s/\s+$//;
print “$_\n”;
}

Give it a name and a keyboard shortcut, and now you have keyboard shortcut for stripping trailing spaces from a document.

by Darren Stone on Oct 16, 2009. #

Vim fans should take a look at cocoa.vim and the Vi Input Manager!

by jon on Oct 21, 2009. #

I found out how to add your own code snippets to XCode by reading this article I found out how and now I add my own code snippets to XCode all the time.

http://www.turkeysheartrhinos.com/?p=8

about 1/2 way down the document read – Custom TextMate Like Macros.

by Kenneth Lewis on Jun 9, 2010. #

#7 is Command-D, not Control-D

Great tips, thanks!

So what do you think of XCode 4? I’m not liking it much right now….

by Derrick on Aug 27, 2010. #

Use CTRL + 1 and CTRL + 2 to open the file list and methods drop downs respectively.

by Rog on Oct 27, 2010. #

Hitting ESC brings up a pop-up window with all possible auto-completion choices. You can hit ESC even with nothing typed, but the menu will be huge. Obviously, this works better once you’ve typed something, and the pop-up menu will show everything that starts with what was typed.

I use this constantly. Great tips on this page, btw!

by anon on Jan 13, 2011. #

Comment out doesn’t work with Xcode 4, apparently. Surely there must be a shortcut for this… but none is listed in help.

by anonymous on Mar 13, 2011. #

Option-clicking the Split Window icon is the most awesome find for widescreen users.

by Alex C on Mar 28, 2011. #

what is the command for commenting a block of code?

command+/ comments each line of selection.

i like to comment code as

/*
need comment
for this
*/

not

//Not like this
//Not like this

by satya on Jun 2, 2011. #

From the menu, Edit – Insert Text Macro – C – Comment Selection. This will use the comment format /* … */

by John Muchow on Jun 2, 2011. #

Bookmark is not longer available for xcode 4! Or did they hide it?

by m3kw on Sep 7, 2011. #

Select a rectangular block of code and delete it…. hold alt and click-and-drag

by Amir Memon on Dec 14, 2011. #