338
votes

With a huge influx of newbies to Xcode, I'm sure there are lots of Xcode tips and tricks to be shared.

What are yours?

89 Answers 89

341
votes

Switch to Header/Source File

  • Option ⌥ Command ⌘ Up Arrow ↑

  • View > Switch to Header/Source File

Switches between the .m and .h files.

  • In Xcode 4 this is ctrl Command ⌘ Up Arrow ↑
16
  • 7
    This is absolutely my favorite, too. I use it constantly. Also works when we are talking about cpp and h. Not just m! :) Commented Sep 30, 2008 at 21:20
  • 24
    It's called "option" on the Mac, not "alt". And if you want fancy symbols, it can be written ⌥⌘↑ Commented Mar 31, 2009 at 15:33
  • 2
    Better yet, open the header, ⌘⌥⇡ to open the .m in the same window, then ⌘⌥⇠/⇢ to switch between them. Commented May 8, 2009 at 19:56
  • 4
    Note that in Xcode 3.2, you have to change the key bindings to restore ⌘⌥⇠/⇢ to switch-file. They changed the default to move between positions in the same file. Commented Sep 14, 2009 at 12:34
  • 71
    A three finger swipe up on the touchpad is another shortcut for the same action.
    – Nathan
    Commented Dec 30, 2009 at 2:07
157
votes

⌘ Command + Double-Click on a symbol: Jump to Definition of a symbol.

⌥ Option + Double-Click on a symbol: Find Text in Documentation of a symbol. (Only works if you have they symbol's Doc Set installed.)

Favorites Bar:

Favorites bar is just like you have in Safari for storing - well - favorites. I often use it as a place to store shortcuts (which you can do by drag-dropping) to files I am using right now. Generally this is more useful when I'm working with a large or unfamiliar project.

To show the Favorites Bar, select the following menu option:

  • View > Layout > Show Favorites Bar
3
  • 11
    Gasp! Show Favorites Bar, where have you been my whole life!
    – willc2
    Commented Jun 28, 2009 at 5:14
  • @Mr.Gando - You can drag-and-drop files on (and off) the Favorites Bar.
    – James Sun
    Commented Nov 17, 2010 at 14:33
  • 4
    The favorites bar has been "replaced" in Xcode 4 with persistent tabs.
    – Ascendant
    Commented Aug 5, 2011 at 5:35
126
votes

Auto-completion Keyboard Shortcuts

Tab ⇥ OR Control ⌃ /: Select the next auto-completion argument.

Shift ⇧ Tab ⇥ OR Shift ⇧ Control ⌃ /: Select the previous auto-completion argument.

Escape ⎋: Shows the auto completion pop-up list.

8
  • Thanks for the Control-/, i was always wondering how to jump to the next arg Commented Jan 23, 2009 at 19:53
  • Holy crap, me too. Until now this was the most annoying "feature" of Xcode. Thanks!
    – pix0r
    Commented Mar 23, 2009 at 16:52
  • Yeah, thanks for pointing that out... not knowing ctrl-/ was killing me
    – Andy White
    Commented Apr 19, 2009 at 0:04
  • Shift-Ctrl-/ advances to the previous argument. Commented Jun 23, 2009 at 2:50
  • 18
    Note that the new XCode uses Tab to move between arguments in completions. It's more fluid.
    – Jab
    Commented Aug 17, 2009 at 17:48
118
votes

Turn off the "undo past the last point" warning

When you attempt to undo after saving, you will get the following prompt:

"You are about to undo past the last point this file was saved. Do you want to do this?"

To get rid of this warning, enter the following into a terminal window:

defaults write com.apple.Xcode XCShowUndoPastSaveWarning NO

Change the company name in template files

Paste this into the Terminal application:

defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "Microsoft";}'

Change "com.yourcompanyname" in all your templates:

  1. Find the directory: /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application
  2. Use your favourite multi-file search-and-replace tool to change com.yourcompany to whatever value you normally use to build for a device. I used BBEdit's multi-find-and-replace after I opened the whole directory. You should be replacing the value in all the info.plist files. I found 8 files to change. The number of times a build has failed because I forgot to change this string is ridiculous.

Quickly jump to a Group in the Groups and Files pane

  • Control ⌃ Option ⌥ Shift ⇧ + <First letter of a Group name>

If you hold down the three keys above, you can quickly jump to groups in the left (Groups and Files) page by pressing the first letter of a groups name. For example, Control ⌃Option ⌥Shift ⇧T takes you to Targets and Control ⌃Option ⌥Shift ⇧S to Source. Press it again and it jumps to SCM. Sometimes it takes several tries for this to work (I don't know why).


Cycling between autocompletion choices

  • Control ⌃ .

  • Shift ⇧ Control ⌃ .: Cycles backwards between autocompletion choices.

Control ⌃. (Control-Period) after a word automatically accepts the first choice from the autocompletion menu. Try typing log then Control ⌃. and you'll get a nice NSLog statement. Press it again to cycle through any choices. To see all the mutable choices, type NSMu then Control ⌃..


Quick Help

  • Control ⌃ Command ⌘ ? (While your cursor is in the symbol to look up)

  • Option ⌥ + <Double-click a symbol>

  • Help > Quick Help

To get to the documentation from the Quick Help window, click the book icon on the top right.


See the documentation for a symbol

  • Command ⌘ Option ⌥ + <Double-click a symbol>

Takes you straight to the full documentation.


Make non-adjacent text selections

  • Command ⌘ Control ⌃ + <Double-click in the editor>

Use the above shortcut for a strange way of selecting multiple words. You can make selections of words in totally different places, then delete or copy them all at once. Not sure if this is useful. It's Xcode only as far as I can tell.


Use Emacs key bindings to navigate through your code

This trick works in all Cocoa application on the Mac (TextEdit, Mail, etc.) and is possibly one of the most useful things to know.

  • Command ⌘ Left Arrow or Command ⌘ Right Arrow Takes you to the beginning and end of a line.
  • Control ^ a and Control ^ e Do the same thing
  • Control ^ n and Control ^ p Move the cursor up or down one line.
  • Control ^ f and Control ^ b Move the cursor back or forward one space

Pressing Shift ⇧ with any of these selects the text between move points. Put the cursor in the middle of a line and press Shift ⇧ Control ^ e and you can select to the end of the line.

Pressing Option ⌥ will let you navigate words with the keyboard. Option ⌥ Control ^ f skips to the end of the current word. Option ⌥ Control ^ b skips to the beginning of the current word. You can also use Option ⌥ with the left and right arrow keys to move one-word-at-a-time.

  • Control ^ Left Arrow and Control ^ Right Arrow moves the cursor between camel-cased parts of a word.

Try it with NSMutableArray. You can quickly change it to NSArray by putting your cursor after the NS, pressing Shift ⇧ Control ^ Right Arrow then Delete.

4
  • Thanks, I hate that "Are you sure..." message. Why is it even there in the first place?
    – ruipacheco
    Commented Oct 22, 2009 at 21:49
  • 4
    Control-Command-double-click is just necessary because Xcode uses Command-double-click for something else other than a discontiguous word selection. In most Cocoa apps you can do discontiguous selection with the Command modifier alone. Commented Nov 1, 2009 at 1:26
  • About the template company name: XCode takes that out of your address card from AddressBook, so if you change it there, XCode will use it.
    – dertoni
    Commented Dec 22, 2010 at 9:32
  • You can also change the company name by setting one for yourself on your Address Book card.
    – macserv
    Commented Feb 3, 2012 at 22:54
98
votes

Open Quickly...

  • Command ⌘ Shift ⇧ D

  • File > Open Quickly...

I'm a big fan of the Open Quickly feature, which is particularly good in Xcode 3.1 and later. When you want to open a file or a symbol definition that's in your project or in a framework, just hit the keyboard shortcut, type a bit of the file or symbol's name, use Up Arrow ↑ and Down Arrow ↓ to pick to the right result (if need be), and then hit Return ↩ to open the file or navigate to the symbol definition.

On Xcode 4:

  • Command ⌘ Shift ⇧ o

Open Quickly uses the current word as a search term

Also, something I didn't know about Xcode until two minutes ago (when schwa pointed it out in a comment) is that, if the editor's text caret is inside of a word when Open Quickly is invoked, that word will be used as the Open Quickly search term.

8
  • Yeah Open Quickly works really well now. Also you can just put the text caret on the file of a #include/#import line and open that file quickly.
    – schwa
    Commented Sep 28, 2008 at 20:31
  • what are these "text caret" you guys talking about?? Commented Mar 16, 2009 at 20:52
  • 9
    On the Mac, a "cursor" indicates your mouse position; the flashing vertical bar is the "insertion point". Commented Nov 1, 2009 at 1:22
  • 1
    @Nicholas Riley: Caret is the right word, AFAIK - our mac developers always called it that when I worked in a software company
    – Polsonby
    Commented May 9, 2010 at 19:17
  • 3
    @Flubba: Caret is certainly used for this, I don't dispute it at all; it's just not the common user- or developer-centric term on Macs. If you Google for "caret site:developer.apple.com" you'll just find hits in TrueType documentation and in a single, very recently written, iPad document. Commented May 9, 2010 at 21:32
78
votes

Use #pragma for organization

You can use:

#pragma mark Foo

... as a way to organize methods in your source files. When browsing symbols via the pop up menu, whatever you place in Foo will appear bold in the list.

To display a separator (i.e. horizontal line), use:

#pragma mark -

It's very useful, especially for grouping together delegate methods or other groups of methods.

3
  • 9
    typing # p will usually trigger code completion '#pragma mark <LABEL>' for quickly adding the label part.
    – willc2
    Commented Jun 28, 2009 at 5:22
  • 1
    It also grabs # TODO statements. Now if only it would also grab # XXX, which is commonly used in the code base I work on...
    – asmeurer
    Commented Dec 28, 2010 at 6:22
  • 7
    Xcode 4 also supports "#pragma mark - Foo" which adds the mark AND a separator at the same time. Commented Aug 4, 2011 at 0:07
76
votes

1. Breakpoint on "objc_exception_throw"

You should always have a breakpoint on objc_exception_throw.

2. Debugging retain/release problems with "Zombie" variables

Use the following code:

NSZombieEnabled = YES;
NSDeallocateZombies = NO;

... to debug retain and release problems. For more information, see the "Finding Memory Leaks" section of Apple's Debugging Applications document.

3. Jumping to a class in Xcode from Interface Builder

Command ⌘ + Double-click on an object in Interface Builder's Document Window to jump to that class in Xcode. This is very handy with File's Owner.

4. Reusing customized objects in Interface Builder

Drag a customized object back to Interface Builder's Library for later reuse.

5. Select overlapping items in Interface Builder

Control ⌃ Shift ⇧ + Click on an object in Interface Builder to see a menu of all of the objects under the mouse.

6. Interface Builder Gesture Guide

Interface Builder Gesture Guide.

1
  • 3
    Great Answer! I'd give two upvotes for (3) and (4) which I didn't know before. Commented Sep 22, 2009 at 7:58
75
votes

Ctrl + 2: Access the popup list of methods and symbols in the current file.

This is super useful because with this shortcut you can navigate through a file entirely using the keyboard. When you get to the list, start typing characters and the list will type-select to the symbol you are looking for.

6
  • 19
    I use Spaces (CTRL-2 goes to Space #2) so I remapped the key binding. It can be found under "Text Key Bindings" in item "Pop Symbols PopUp."
    – bbrown
    Commented Apr 10, 2009 at 21:16
  • 1
    Nice tip. Made me discover that Control-1 will bring up the file history list.
    – tmadsen
    Commented Feb 8, 2010 at 7:28
  • 2
    In XCode5 it's CTRL+6 I believe
    – Ran
    Commented Apr 5, 2011 at 5:59
  • 1
    Is there a keybinding for this in XCode 4? I'm using Spaces, so I'd like to rebind this, but I can't find it. Commented Apr 8, 2011 at 1:41
  • 5
    In XCode4 it's ^6. It's called Show Document Items.
    – WBlasko
    Commented Jul 5, 2011 at 2:02
73
votes

Zoom Editor In

If your window displays both the detail and editor view, you can zoom the editor in to expand the editor view to the full height of the window. (This is fairly easily found, but many seem to overlook it.)

You can do this by using one of the following methods:

  • Command ⌘ Shift ⇧ E

  • View > Zoom Editor In

  • Drag the splitter (between the editor window and the file list above it) upwards.

1
  • 18
    It took me AGES to find out how to hide the tree as well - Command-Option-Shift-E. Ah, sweet relief. I'm forever indebted to the MacMacDev Glasgow group for letting me know this. Commented Oct 12, 2009 at 9:01
66
votes

Get Colin Wheeler's Complete Xcode Keyboard Shortcut List (available as PDF or PNG). Print it and keep it somewhere visible (I've got it on the wall next to my screen).

edit: Updated versions for Xcode 3.2

edit 2: Updated versions for Xcode 4

51
votes

Not much of a keyboard shortcut but the TODO comments in the source show up in the method/function dropdown at the top of the editor.

So for example:

// TODO: Some task that needs to be done.

shows up in the drop down list of methods and functions so you can jump to it directly.

Most Java IDEs show a marker for these task tags in the scrollbar, which is nicer, but this also works.

5
  • 4
    I just tried // MARK: and it has the exact same effect as #pragma mark. You can even use // MARK: with a dash to get the separator. Commented Sep 2, 2010 at 4:12
  • 1
    I wish you could add markers to the list of things it catches. The code base I work on has a lot of # XXX, and it doesn't see those.
    – asmeurer
    Commented Dec 28, 2010 at 6:23
  • @Gonzalo Larralde What? I don't mean writing // TODO, I mean seeing all the TODO comments in the dropdown bar. #failatfailing
    – ingh.am
    Commented Jun 22, 2011 at 12:04
  • 5
    In Xcode 3 TODO comments can be placed anywhere in the code and they will appear in the function popup. For some reason in Xcode 4 TODO comments must be outside any function/method body to appear. It's a bug.
    – SteveCaine
    Commented Sep 27, 2011 at 20:31
  • @SteveCaine months later, is it still a bug, or is it now just reality? Commented Feb 12, 2012 at 23:04
50
votes

⌘-[ and ⌘-] to indent and unindent selected text. Makes cleaning up source code much easier.

3
  • If you cut the text and paste it back, it will magically reindent properly. :)
    – Neo42
    Commented Sep 17, 2009 at 16:30
  • 14
    Or just select it and hit control-I (like tab but not really). Commented Nov 1, 2009 at 1:35
  • Is that a control shift i? or a control l (l is for lowercase) ?
    – VxJasonxV
    Commented Feb 9, 2011 at 0:30
43
votes

Hold Option while splitting windows to split vertically rather than horizontally.

0
41
votes

Double-click on the square brackets or parentheses to obtain bracket and parentheses matching.

0
35
votes

Control Xcode's text editor from the command line: xed

> xed -x                # open a new untitled document
> xed -xc foo.txt       # create foo.txt and open it
> xed -l 2000 foo.txt   # open foo.txt and go to line 2000

# set Xcode to be your EDITOR for command line tools
# e.g. for subversion commit
> echo 'export EDITOR="xed -wcx"' >> ~/.profile

> man xed               # there's a man page, too
2
  • Is this specific to certain versions of Xcode? xed doesn't seem to exist for me (SnowLeopard, recent Xcode/iOS SDK)
    – ipmcc
    Commented Nov 28, 2011 at 13:46
  • From the man page: xed was introduced in Mac OS X 10.5 with Xcode 3.0. Commented Feb 6, 2012 at 20:50
31
votes

"Ctrl+Left/Right Arrow" to do intra-word text navigation. I use this feature to jump the cursor from the one "camel hump" in a variable to the next.

6
  • 10
    It's great so long as you have Spaces disabled :\
    – jbrennan
    Commented Aug 17, 2009 at 17:58
  • Also try Option-f, Option-b, Option-d etc. These are all emacs style keybindings.
    – ustun
    Commented Oct 14, 2009 at 18:33
  • 2
    @jbrennan I have Spaces assigned to ⌃+⌥+⇧+⌘ Edit: To set Spaces to ⌃+⌥+⇧+⌘, select the "To switch between spaces:" and the "To switch directly to a space:" popup and hold down the Control, Option, Shift and Command keys.
    – jrtc27
    Commented Jul 12, 2010 at 9:51
  • Been looking for this for a while. Thanks Matt, and thanks jtc27 for the Spaces tip!
    – Reed Olsen
    Commented Nov 11, 2010 at 22:19
  • @ustun: Option-f for me types ƒ.
    – asmeurer
    Commented Dec 28, 2010 at 6:25
30
votes

Xcode supports text macros that can be invoked via the Insert Text Macro menu at the end of the Edit menu. They can also be invoked using Code Sense, Xcode's code completion technology.

For example, Typing the key sequence p i m control-period will insert #import "file" into your code, with file as an editable token just like with code completion.

0
29
votes

Right click on a variable in your function and click edit all in scope. Been using it a lot since I found this out.

ctrl T

4
  • Very useful, no idea it was there. That contextual menu is definitely worth exploring.
    – ruipacheco
    Commented Oct 22, 2009 at 21:58
  • 7
    You can access this using Ctrl-Command-T when over the word you want to edit. Much faster than a pesky menu! Commented Nov 27, 2009 at 17:23
  • It is called Ctrl+click on a Mac.
    – user142019
    Commented Feb 6, 2011 at 2:18
  • 3
    The default shortcut changed to Ctrl+Command+E in Xcode 4, and can be changed in the key bindings preferences.
    – Dov
    Commented Apr 13, 2011 at 19:28
28
votes

You can have Xcode run the preprocessor over your Info.plist file:

        <key>CFBundleShortVersionString</key>
    #ifdef DEBUG
        <string>1.0 (debug)</string>
    #else
        <string>1.0</string>
    #endif

See http://developer.apple.com/technotes/tn2007/tn2175.html for details.

1
  • 16
    Note that if you do this, your Info.plist will always have to be edited as text; you won't be able to edit it in the nice Property List Editor that keeps it using correct keys and value types. Commented Oct 1, 2008 at 7:46
26
votes

Navigate among open files back and forth:
⌥⌘←
⌥⌘→

4
  • 5
    The key is "option" (⌥) on the Mac, not "alt." Commented Oct 4, 2008 at 19:14
  • 7
    The white Apple keyboard does indeed have alt written on the option key. Commented Oct 19, 2008 at 2:35
  • 19
    Chris, the 'alt' label is for switchers. Old school Mac guys know it as Option. Like God intended.
    – willc2
    Commented Jun 28, 2009 at 5:18
  • 1
    If you're a fan of trackpad gestures, you can also use a three-finger swipe to the left and right to get the same effect.
    – Reed Olsen
    Commented Aug 27, 2010 at 17:46
26
votes

Debugging - how to use GDB

Being new to this still, I find trapping and identifying faults a rather daunting job. The console, despite it being a powerful tool, usually does not yield very intuitive results and knowing what you are looking at in the debugger can be equally difficult to understand. With the help of some of they guys on Stack Overflow and the good article about debugging that can be found at Cocoa With Love it becomes a little more friendly.

0
25
votes

Technically an Interface Builder tip, but they're a book-matched pair, so I don't think this is off topic...

Shift + Right Click on one of your controls and you get a nice pick list of the object hierarchy. No more click, click, click, frustration!

22
votes

With Trackpad:

  • Swipe Three Fingers Up - Switch between header and source file, which is easier than Cmd + Opt + Up;
  • Swipe three fingers down - Switch between declaration and definition when selecting a class or method, found these two kind currently;
  • Swipe three fingers left - Go back (Cmd + Opt + Left);
  • Swipe three fingers right - Go forward (Cmd + Opt + Right);

Tested with Xcode 3.2.5.

21
votes

The class browser in Xcode! Reached by pressing shift + + c. You can reduce the scope to only show your active project. It gives you a less cluttered view as long as you only want to browse the class hierarchy.

2
  • Wow! Did not know this one... this may become my main "working" window. Almost feels like VisualAge (the good parts) :D
    – rcw3
    Commented Jan 19, 2010 at 21:48
  • AAARRGGHH As an old ObjectMaster fan, I was happily reading about this until I realised it was removed in XCode4, can't believe what a giant step backwards this IDE is. I bet there's a ton of people inside Apple still using Xcode 3.
    – Andy Dent
    Commented Jan 24, 2012 at 14:04
19
votes

Using ] to automatically insert [ in the correct location

I come from a .NET background, so I'm used to typing a symbol and then typing one of its method names. So I always forget to include the [ before I start typing the object name. Usually this meant I would need to go to the beginning of the line and add the [ manually. I didn't realize I could just press ] at the current cursor position, and it will be added automatically.

There are ways to use this: either after typing the function's name, or right before typing the function's name.

Method 1: after the function name

myObject testMethod]

... becomes:

[myObject testMethod]

... with the caret positioned after the ].

Method 2: before the function name

myObject]

... becomes:

[myObject ]

... with the caret positioned right before the ].

The advantage of the latter (2) is that code completion will filter on the methods of your object. Whereas with the former (1) if you try to invoke code completion immediately after myObject, it won't be filtered. Another advantage to (2) is it behaves more like other programming languages that use dot notation. You type the name of the object then simply ] instead of . to access a method.

0
17
votes

Select a block of text and type cmd-/ to comment it out. Do it again to remove the comments characters.

This is especially useful when combined with brace-matching by double-clicking on balanced chars (parens, braces, brackets).

1
  • The Xcode commenting/uncommenting was hoplelessly broken last time I checked. It's a shame.
    – Joe
    Commented Oct 20, 2011 at 13:58
16
votes

Being able to quickly see all the methods that can be overriden from a super class. For example when extending UITableViewController I just type in my implementation:

- ta

and then I hit ESC to see all the methods from my superclass that begin with "ta" such as

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

This also works when adopting protocols.

0
14
votes

Command ⌘ alt ⌥ shift T : reveal the current edited file in the project tree.

1
  • fantastic tip, thank you. This is so useful when working on someone else's code, like when you are called in to work on a project after it has been in development a while. Commented Oct 7, 2010 at 23:28
13
votes

ctrl + alt + + r to clear the log

1
  • 1
    Interesting, I thought Apple frowned on shortcuts that needed both hands to execute? Commented Apr 4, 2011 at 15:03
13
votes

When using Code Sense with many keyboards, use control + , to show the list of available completions, control + . to insert the most likely completion, and control + / & shift + control + / to move between placeholder tokens. The keys are all together on the keyboard right under the home row, which is good for muscle memory.

1
  • 2
    instead of using control-comma for the list of available completions, you could use esc also.
    – tmadsen
    Commented Feb 8, 2010 at 7:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.