Xcode – Keyboard Shortcuts & Productivity Tips

By Eddy Chung

Xcode is a program with so many options and features. Investing some time into learning some of the shortcuts and tricks will save you time in the long run. I’ve been a professional iOS developer for a couple of years now, so I’d thought I’d share some of my productivity tips in Xcode and Swift.

Xcode Keyboard Shortcuts and Hotkeys

Open Current File: CMD + Shift + J

This opens your current file in the project navigator. It’s one of my most used shortcuts. Opening the current file in the project navigator allows you to browse files that are in the same folder which you’ll likely need. This enables you to browse through the codebase more easily without getting lost. If your project navigator is closed, this command will also open it up.

Clean Build Folder: CMD + Shift + K

This keyboard shortcut executes the clean build folder command on your project. Sometimes your project is not building for one reason or another. I always run this command before I start debugging or diving into the potential problems. I’d say 95%+, build problems can be resolve just by running a simple clean. This command will save you a lot of headaches and time!

This shortcut is handy for navigating quickly to the top of the file or the to bottom of the file. It helps when working with smaller code files. Learning these shortcuts can improve how fast you read code.

As you write more code, you’ll develop patterns of having certain functions near the bottom of the file and properties near the top.

Documentation Comment: CMD + OPTION + /

Create a quick documentation style comment template for you to fill in. This is useful to document complicated functions. Don’t over use these comments, ideally your function’s name and its parameter names should explain themselves. Try to use this for only complicated functions.

Comment: CMD + /

I use this shortcut all the time. It comments out the line you have highlighted. Great for debugging by removing certain sections of code or to test how something will behave with blocks of code commented out.

Show Inspector Panes: CMD + Option + 1,2,3,4…

This keyboard shortcut opens up Inspector Panes. I use this command a lot when I’m working inside the interface builder. This tip allows me to modify attributes of the UI elements I am working on quickly. Honestly, I don’t remember how to open this menu up without the short-cut anymore :).

I use CMD + Option + 4 the most often as this is the Attribute Inspector pane. This pane has most of the options I use when working inside the interface builder.

Build and Run: CMD + R

This shortcut starts the Build and Run command. Probably one of my most often used shortcuts as well. You’ll be executing this command a lot while you’re debugging or building code.

Find in Workspace: CMD + Shift + F

This is the Find in Workspace command. If you’re working in anything other than a small codebase, you’ll find this search shortcut extremely valuable. This enables you to navigate through the codebase at a much faster space. The larger the codebase, the more useful you’ll find this command.

Show Navigator Panes: CMD + 1,2,3,4,5,6

These shortcuts will navigate through the different navigator panes. They’re number 1 through 6. I find myself using Cmd + 1 the most to open up the Project navigator.

New File: CMD + N

This command creates a new file and is typically of many apps, not just Xcode. This saves me some time when I’m creating a new Swift file or a new nib file.

Show UI Object Library: CMD + Shift + L

Opens up the UI Object library in the interface builder. This allows you to select different objects in the UI library such as labels, buttons and other interface controls. This shortcut is useful when you’re trying to build user interfaces very quickly.

Show UI Object Library & Persist: CMD + Option + Shift + L

This opens up the UI Object library but also persists it in its own separate window. Useful when you want to keep this window open, just like older versions of Xcode.

Show Media Library: CMD + Shift + M

Shows your media library allowing you to add images quickly to your user interface. Also allows you to search through your media library to see what you have available.

Jump To Definition: Control + CMD + Click

This command will jump to the definition of any function or variable. This is again useful when navigating through large codebases. I use this command a lot when I’m learning a new part of the codebase or debugging an existing feature.

Show/Hide Debugger Pane: CMD + Shift + Y

This keyboard shortcut will hide or show the debugger area. The debugger is an essential feature for most developers as it greatly speeds up debugging. I like to hide away this section when I’m reading code.

Jump to line: CMD + L

This shortcut allows you to quickly jump to a line in the current file. This is helpful when you’re working on a team and somebody specifies a certain line number they want you to look at.

New Tab: CMD + T

Creates a new tab where you can have another editor window. Tabs can be later pulled into separate windows if desired.

Navigates through your tabs forward or backwards.

Quick Documentation: Option Click In Code

This will open up a documentation fly out. This allows you to quickly read and understand what a function call does. If you like to read more there is an option to open up the full developer documentation.

Option Highlighting

Hold option and hover over any bracket. This will quickly highlight all the code between the open and closing bracket. Super useful when working within many nested brackets.

Option Click on File

Quickly opens a file in the assistant editor when you option-click a file in the project navigator. Super useful when you need to compare two files quickly.

Open Quickly: CMD + Shift + O

Quickly open any file in your project. One of my favorite shortcuts! Especially useful if you’re working in a very large codebase.

Fix indentation: CMD + I

You ever have some code that’s been all messed up indentation wise? This command will quickly fix the indentation making your code pretty and more readable.

Edit All In Scope: Ctrl + CMD + E

If you want to rename a variable or function, this shortcut will allow you to edit all instances of it at the same time.

Simulator Keyboard Shortcuts

Show Keyboard: CMD + K

This brings up the keyboard in simulators. It’s important to test how your application looks like when the keyboard is on the screen. A common bug is having critical user interface elements hidden behind the keyboard. This can lead to a very frustrating user experience.

Rotate: CMD + Left Arrow or Right Arrow

This rotates your simulator in a clockwise or counter-clockwise direction. This is critical to ensure your application performs as expected in both orientations. Sometimes user interface elements can be misaligned or in the wrong position when changing orientations. Remember, the user can rotate their device at any time while your application is open.

Xcode Tips

Code snippets

Have you found yourself rewriting the same code over and over again? Code snippets are your answer to saving some time. It’s under Editor > Create Code Snippet.

You can then assign an autocomplete keyword to have the code autocomplete every time you type this code out.

Creating Errors and Warnings

To create a warning or error in your code, just use #warning or #error. If you put a warning in, it will show up in your Issue Navigator, allowing you to keep better track of it.

If you use an error, your build will not compile. Here are some code examples:

#error ("this is an error")
#warning ("this is a warning")

In Xcode’s search tab, you can create a new Scope. This new scope allows you to filter your search down. A very common filter I use is files ending in .swift or .m. This allows me to search all Swift files or all Objective-C files separately.

make_46

make_47

Filter Within Search Results

Another search tip is to use the filter text field at the bottom of the search results. I didn’t find out about this until a couple months into iOS development. It’s super useful to filter your current search results quickly instead of having to perform a new search every time.

make_48

Show Only Errors and Hide Warnings

This tip is more useful for iOS developers working on large codebases where there can be a lot of warnings. With all the warnings it can be hard to see the errors and what is causing your build to fail. There’s a small but very useful button at the bottom of this pane which allows you to hide all warnings and show only errors. Next to that button, there is another icon that allows you to show only issues from the latest build.

make_49

See Different Screen Sizes in the Interface Builder

It’s important to make sure your user interface appears correctly on different screen sizes. Xcode makes this easy by allowing you to view your layout in different screen sizes. In the interface builder, theres a pane in the bottom left of the main editor window. In this pane, you can select different devices including all kinds of iPhones and even iPads. There are also options to view both orientations so you can be sure your app looks as expected in portrait and landscape mode.

make_50

Refactor Automatically

Refactoring in Xcode has been made easier over the last couple versions. Now when you’re renaming a function, Xcode can change all the references to that function automatically. This also works for variables and class names. After you’ve highlighted the name of the object you want to refactor, simply right click and select the Refactor > Rename option.

make_51

Extract function

You can extract function and refractor it by highlighting the function, right-clicking and selecting extract method. Xcode will automatically create an extracted function for you and call it in the method. Then you can rename the function as required. make_52

MARK Comment

Adding a // MARK: in your code will allow you to break up your code into different sections. This will allow you to organize your code better in a single file.

make_53

Split Screen with Assistant Editor

Using the split screen assistant editor makes it easy to work with two files at once. This is especially useful when connecting IBOutlets to their respective Swift. It’s also useful when you’re trying to compare to different files and determine if you can refactor them or not.

To access the split screen editor, select the icon that looks like two circles near the top right hand corner of Xcode. To go back to a single editor, select the Icon that looks like a bunch of lines.

make_54

Automatically Move Elements to Satisfy Constraints in the Interface Builder

When you get orange warning lines in your Interface Builder, that means some elements are not where they should be according to the constraints you’ve set. You can manually move these elements into their correct position. However, this can be particularly frustrating every time something is a couple of pixels off. Moving these elements to their exact correct location seems like a waste of time. There is a better way to do this however and it’s as simple as clicking the update frames button at the bottom right-hand corner.

When you click this button Xcode will automatically move all elements to their correct position according to the constraints you’ve set.

make_55

If you liked this post, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts