Xcode navigation tricks

Navigating code is one of those things where you accept that it is sometimes painful and slow but just get on it with it without a second thought on how to improve the experience. I’ve been using Xcode for a long time and I’ve got many different strategies/techniques that help me navigate a project effectively. Here’s the ones I use most often.

Project navigation

Getting roughly to the right place in a project can be really challenging in a large code base where the project navigator doesn’t fit on one screen. Here’s the techniques I use for broad brush navigation.


Open quickly

Hitting the combination ⌘+⬆+O will launch the Open quickly dialog box. This is a good first port of call when you can roughly guess the name of something - start typing and hopefully the list will filter down to what you are looking for or trigger other ideas on what to search.

To make this more powerful I utilise the fact that the search term doesn’t need to be 100% precise for example if I’m looking for a view and have a suspicion it might be named something like HomeView I can straight away include view in my term and just prepend h and I might already get a pretty short candidate list like below:

HeaderView.swift
HomescreenViewController.swift
HelpViewController.swift
HiddenSettingsView.swift

This is often close enough to help me narrow in on what I want.


View debugging

Sometimes I really just can’t figure out what something will be named and that’s where I reach for view debugging. I can navigate to the screen I want to locate the source for on a device/simulator and then hit the Capture View Hierarchy button in the Xcode debugging toolbar. Now I can navigate around the view hierarchy and see the names of views and view controllers. Once I have the name I go back to the trusty Open quickly dialog and I’m there.

This is such a simple technique but really powerful - I’ve recently been retrospectively adding accessibility ids to a large old codebase and this has really allowed me to drill in quickly make changes and move on.


Project find

Hitting ⌘+⬆+F lands you on the Project find pane where you can execute various searches of your project. There’s loads of options you can play with that I won’t cover here but it’s worth familiarising yourself with all the capabilities. The settings I play with most often are scopes to only search in certain projects/folders or changing to regex for when plain text search isn’t flexible enough.

Bonus tip: You can delete the items in the search results by selecting it and hitting back space. This allows me to treat a search like a todo list I can filter down as I do work.


Filter bar

At the bottom of the Project navigation pane there is a filter bar which I don’t use often but when I do it’s very handy. Being able to hit the +- button and filter by only files that have source control changes is great when you want to focus on tidying up work before committing.


Reveal in project

Once I’ve found a helpful class I instinctively hit ⌘+⬆+J to reveal in project. This expands the Project Navigator to show where the file is located, this is often helpful as related code tends to be grouped together. Seeing the file you are looking at in the context of similar classes can often help ground you and give you an idea of other things you might need to explore.


In file navigation

The challenges with navigation don’t necessarily stop once you’ve found a source file. Here’s the tricks I use when navigating in a single file.


Jump bar

If I’m new to a class and I want to see what methods it has available my go to technique is to use the jump bar with ctrl+6. This jump bar lists all the methods available and just like Open quickly I can start to filter the methods without needing to be too precise with my guess. This is a great way to get a sense of how a class is structured and filter to methods of relevance quickly.


We can hit ⌘+F (without the ) to search within a file. This is great but often I am searching for a word I have highlighted so if I use this technique I actually need to do ⌘+C, ⌘+F, ⌘+V and then hit enter. That’s far too much effort luckily if you have a word selected you can hit ⌘+E to add the word to the global search and then hit ⌘+G to cycle through the matches. If you go too far or have a feeling that navigating bottom to top would be faster then add a shift into the mix to change direction ⌘+⬆+G.

Bonus tip: ⌘+E is the global search so most well behaved text fields on a mac will work with this e.g. I can select a word in Safari hit ⌘+E then go to Xcode and cycle through matches with ⌘+G.


Jump to definition

A large percentage of our time is spent reading and understanding code. The ultimate short cut for this is ⌘+^+j to navigate to the code that defined a symbol or a method.

Bonus tip: jumping around is great if you can find your way back so remember ⌘+^+← to jump back in your navigation stack and ⌘+^+→ to navigate forwards.


Undo/Redo

In large files it’s often the case that you can be writing code in one place and then need to reference another part of the same file - probably using a combination of these techniques. Instead of trying to find my way back to the code I was working on by manually scrolling I instead hit ⌘+Z to undo my last change and then immediately hit ⌘+⬆+Z to redo the change. This combination might seem pointless but the side effect is that the editor will jump the caret back to the place being edited.


Conclusion

There’s loads of ways to navigate - the above list isn’t exhaustive it’s just the things I use most often. I love watching other people develop and I’m often the annoying person stopping things to say “what was the keyboard shortcut” or “how did you do that”. The above is my attempt to share some things that I would find interesting if someone else did it in front of me.