At the third Lightning Talks, I ate a Scotch bonnet pepper and presented 13 tips and tricks about Xcode. If you missed the talk, or wanted to know about one of the slides in more detail, here’s a video and a written version of everything I mentioned, plus a few extras.
Spicing Up Xcode was inspired by Hot Pepper Gaming, a spectacular YouTube channel that answers the age-old question of what would happen if a bunch of professional game reviewers ate hot peppers and then reviewed video games.
Mac Keyboard Symbols
First, a refresher on some of the more obscure keyboard symbols.
← ↓ → ↑ Arrow Keys
Spicing Up Xcode – Keyboard Shortcuts
1. Control-Arrows to Jump by Camel Case
You can use ⌃→ and ⌃← to jump forward and backward by camel case substrings in any text in the editor. For example, if your insertion point is at the beginning of tableView and you want to jump to between table and View, press ⌃→ and the cursor will move right one camel case substring. This also works in combination with the standard OS X text navigation shortcuts. For example, press ⌃⇧→ to select the next camel case substring, or ⌃delete to delete the previous camel case substring.
Note that you may have to disable OS X’s default keyboard shortcuts for moving between Spaces, which use the same key combinations. You can do this under System Preferences → Keyboard → Shortcuts → Mission Control, and uncheck the boxes next to Move left a space and Move right a space.
Sadly, jumping by camel case is not supported in Xcode’s native text fields (such as the Search field), nor elsewhere in OS X. I’ve filed a radar.
2. Balance Delimiter
This Xcode command is located under Editor → Structure → Balance Delimiter. It selects the next outermost set of brackets, braces, or parentheses from the current selection. This is similar to the behavior where double-clicking a bracket, brace, or parenthesis will select its counterpart and everything in between. You can see both behaviors in this screen recording:
You can set a keyboard shortcut for Balance Delimiter in Xcode Preferences → Key Bindings. My shortcut of choice is ⌘⇧Space.
SPICING UP XCODE – Navigation
Xcode has dozens of commands and keyboard shortcuts to support many different kinds of navigation. You owe it to yourself to spend a while browsing through the View, Find, and Navigate menus to see what’s available. Try holding ⇧, ⌥, and ⇧⌥ to see what hidden commands are lurking in there!
It would take several blog posts to cover all of the navigation shortcuts, but here some of my favorites:
3. Reveal in Project Navigator: ⌘⇧J
Opens the left sidebar, shows the Project Navigator, and selects the file you are currently editing. Convenient for seeing where it is in the project, and what its siblings and related files are.
4. Show Documentation: ⌥ Click
Option-click any method to show a popup with the documentation for that method. If you’re using a trackpad, you can also three-finger tap on the method. The best part: this works with all your own /** documentation comments */ (like the ones you can create using VVDocumenter-Xcode).
5. Open Documentation: ⌥ Double Click
If you want to open the full documentation browser for a particular method, Option-double click it. This is particularly useful for some of the core Apple framework methods, such as -[UIView isOpaque], since their long Discussion sections don’t appear in the popover mentioned above.
6. Open in Other Editor: ⌘⌥,
When you are editing a file in the primary editor, this shortcut will open it in the assistant editor. When you are in the assistant editor, it will open the same file in the main editor. This is useful when combined with other shortcuts to open files in various editors. For example, if you are working on a file in the assistant editor, but you want to promote it to the main editor before you open a new file in the assistant editor, this shortcut makes it easy.
7. Jump to Definition in… ⌘⌥⌃ Click
Like the above command, this one is contextual: whichever editor you are in, it opens a file in the other. However, instead of opening the current file in the other editor, it opens the definition of the symbol you click.
8. Better Navigation Through Trackpad Settings
OS X trackpad gestures have come a long way since they were introduced. Originally, you swiped with three fingers to navigate: left to go forward, right to go back. Later, this was replaced by a two-finger continuous drag gesture in the opposite direction. This shortcut fits better with the “natural” scroll direction introduced in OS X Lion, but it can be annoying to have to sit through a swipe animation when you want to quickly jump between files.
Luckily, by twiddling a couple of trackpad settings, you can get the best of both worlds: the pretty, continuous two-finger transitions, and the instantaneous three-finger swipes. Note that they go in opposite directions from each other, but as long as you can keep that straight, you can use them interchangeably. To change these settings, go to System Preferences → Trackpad → More Gestures, and make the following changes:
- Swipe between pages: Swipe with two or three fingers. Enables three-finger swipes.
- Swipe between full-screen apps: Swipe left or right with four fingers. Frees up three-finger swipes from having to switch spaces, which you can now do with four fingers.
- Bonus setting: change Mission Control and App Exposé each to use four fingers (or disable them if you don’t use them). This will allow you to swipe up or down in Xcode to switch between a class’s .h and .m files. The keyboard shortcut equivalents of these are ⌃⌘↑ and ⌃⌘↓. (Are going “up” and “down” the same? For normal .h/.m pairs, yes, but if you have .h, .m, and _Private.h, the shortcuts cycle through them forward and backward.)
9. Open Quickly ⌘⇧O
Anywhere in Xcode, type ⌘⇧O (as in oh, not zero) to bring up the Open Quickly dialog box. You can type the name of any file or symbol, and it will do a fuzzy match of everything in your project, plus all of Apple’s public headers. It searches file names, methods, functions, properties, and constants. The “fuzzy” part means that, while you have to get the letters in the right order, you can leave some out.
In the example below, the file I’m looking for is called INVEditCreditCardsButtonView.xib. I can’t remember the full name, but I know it had something to do with editing cards, and maybe it was a button, so I type “editcardbutton” into Open Quickly, and the first few results are pretty close to what I want:
But remember, I wanted the xib. I could press the down arrow key twice, or use the mouse to double click the third result, but there’s an easier way. Since the file extension is taken into account in this dialog box, I can just add an additional “x” to my search in order to highlight the correct file, and then press Return to open it.
SPICING UP XCODE – Tricks
Here are a few miscellaneous tricks that can greatly speed up your work in Xcode.
10. Move the Program Counter
When you are stopped on a breakpoint, there is a small green arrow shape that sticks out of the gutter on the left. This represents the program counter, and it points to the instruction that is about to be executed. The cool thing is that you can click and drag the program counter anywhere else in your program! This can be useful if you want to jump over a line without executing it, or perhaps go back and run a line again. With great power comes great ability to crash in new and spectacular ways, but if you’re careful, this can be a great time saver as an alternative to editing and recompiling.
11. Preview Assistant Editor
If you are using trait collections in iOS 8+ apps, your xibs and storyboards are probably all 600 × 600 pt squares, and it can be hard to visualize how they will look at different sizes. Luckily, the Assistant Editor is here to help.
Open the Assistant editor (⌘⌥↩). From the Jump Bar, choose Preview.
This will give you a preview pane with a device-specific view of your interface. You can click the + button at the bottom to add more screen sizes, allowing you to test your layouts with different widths, heights, and aspect ratios.
12. Searchable Menus
On a Mac, you can open a popup menu and type the first few letters of an item to jump to that item. You’ve probably used it to find your state or year of birth on Web forms. However, Xcode menus go one better. Most menus in Xcode are fully searchable. Just start typing, and a search field will appear, filtering the menu down by the characters you’ve typed. And the best part is that searchable menus employ the same fuzzy matching as the Open Quickly dialog.
A great use of searchable menus is in the Show Document Items menu, which you can activate using ⌃6. This menu, which appears from the Jump Bar, shows all the classes, properties, and methods in the current file. It also shows dividing lines and section markers that you’ve inserted manually using #pragma mark - Section Name.
Just start typing, and you can quickly filter this menu to select the item you want:
Alcatraz is a package manager for Xcode. It lets you browse and easily install plugins, templates, and color schemes. I mentioned Alcatraz in the talk, but I wanted to use the extra space here to list some of my favorite Xcode plugins.
- DerivedData Exterminator
As your project grows, Xcode seems to lose its ability to keep track of all the files and sub-projects that make up your app, and various build products seem to get stale and need to be blown away. This plugin puts a button in the Xcode toolbar that makes it a snap.
This plugin supercharges Xcode’s CodeSense autocomplete system by adding fuzzy matching, just like you can already use in Open Quickly and searchable menus. It’s particularly good for Objective-C code, which often has many elements with long, similar names that differ only near the end. With FuzzyAutocomplete, if you’re trying to type a name like UIViewAutoresizingFlexibleTopMargin, you can just type, for example, flextop. FuzzyAutocomplete takes care of the rest. I filed a radar for Apple to incorporate this into Xcode, because it should work like this out of the box.
Xcode’s Comparison view is useful for seeing which lines have Git status, but it’s also cumbersome (it takes up half your screen). GitDiff provides a lightweight alternative: small colored bars that live in the gutter alongside line numbers and breakpoints. When I’m using it, I have a clear and unobtrusive reminder of which lines I’ve edited, which can help me to keep track of what I’m currently working on.
Type a switch statement, and this plugin will write all the cases for you, based on the values defined in your enumeration. Another one that should be built in.
Type /// above any method or property, and this plugin will transform it into a formatted documentation comment, complete with placeholder parameters for the arguments and return value. Best of all, these comments work with the ⌥ Click documentation popover trick described above.
Vim shortcuts and editing modes in Xcode. Useful if you like pain. (Can you tell I’m not a Vim user? But it’s super powerful if you already know the shortcuts.)
What Are Your Favorites?
I hope these tips and tricks have been helpful. There is so much more depth and breadth to Xcode than the small amount I was able to cover here. What are your favorite tips and tricks? Post them in the comments below!
Interested in joining the Raizlabs team making great software? We’re hiring developers in Boston and SF.