Boost Your Productivity with These Xcode Keyboard Shortcuts

Boost Your Productivity with These Xcode Keyboard Shortcuts

What’s awesome about the tips & tricks talked about in this article is that a lot of these quick key commands will apply to other applications. Most applications try to keep things unified so there is some familiarity for the user. With that being said, this is mainly written for Xcode.

Lastly, take a few minutes and a few tips from this article each day and open up a document or project, and tinker with it. There’s a lot here. The point isn’t to remember everything right away, but to build some muscle memory. This will only happen when you actually get yourself to use these key commands and you’ll be glad you did!

Cheers 🍺!


Symbols

The symbols we'll be using are as follows:

  • Command Modifier:

  • Shift Modifier:

  • Option Modifier:

  • Control Modifier:

  • Arrow Keys: ← → ↑ ↓

  • Return Key: ↩︎

Getting Around Your Code

  • Jump to the top or bottom of the file: ⌘ ↑ or ⌘ ↓

  • Jump to the beginning or end of a line: ⌘ ← or ⌘ →

  • Jump to the beginning or end of each word: ⌥ ← or ⌥ →

  • Move up & down 1 line at a time: ⌥ ↑ or ⌥ ↓

    • Moving up in this way will always keep the caret at the beginning of the line

    • Moving down in this way will always keep the caret at the end of the line

Selection

  • Select everything above or below the caret position: ⌘ ⇧ ↑ or ⌘ ⇧ ↓

  • Select line above or below: ⇧ ↑ or ⇧ ↓

    Note: Xcode will try its best to match the current caret position from the previous line to the line above or below (depending on which way you choose to go) and the selection will take place up until that position. "Try its best" means it'll work if the next line is the same length or more. If it's shorter, then Xcode will just select the whole line. What's neat is if you select 3 lines using this method and the middle line's length doesn't match the 1st and 3rd line, the selection will still end on the 3rd line where the selection started on the first line. Aside from that, You can easily select the rest of the line by using the next bullet point.

  • Select an entire line or everything to the left or right of the caret depending on caret position: ⌘ ⇧ ← or ⌘ ⇧ →

    Note: To select the entire line above or below it’s best to have your caret at the beginning of the line. If you don’t, you'll get the same behavior as in the last Note.

  • Select by word to left or right: ⌥ ⇧ ← or ⌥ ⇧ →

The next key command will allow you to select by words inside names that are using camelCase, PascalCase, or even snake_case. Pretty awesome if you ask me. The only thing with snake case is that it will stop and select each underscore _ as well. Still awesome.

  • Select by word inside special case left or right: ⌃ ⇧ ← or ⌃ ⇧ →

  • Select by character left or right: ⇧ + ← or ⇧ + →

Moving Your Code Around

Xcode

  1. First place the caret on the line you want to move

  2. Move line up: ⌘ ⌥ [

  3. Move line down: ⌘ ⌥ ]

  4. Indent or Outdent line of code (whatever line the caret is on): ⌘ [ or ⌘ ]

VSCode

Bonus Tip — I decided to include VSCode because there is a slight difference. I had to use it when studying computer science and using Python, and I know some iOS developers that are in VSCode from time to time.

  1. First, place the caret on the line you want to move

  2. Move line up: ⌥ ↑

  3. Move line down: ⌥ ↓

Modifying Your Code/Text Quickly

If you want to quickly add quotes ””, square brackets [], or parenthesis (), you can do this by selecting the code or text you wish to encapsulate using one of the above methods. Once your selection has been made, just enter the left of the pair. The right quote, square bracket, or paren will end up replacing your text with whichever right ", ], ), } symbol you chose.

Example

Let’s say I have this code: let reuseIdentifier = UserCell. As we all know, a tableView’s reuse identifier needs to be a string and you can see I forgot to add quotes around the value. All I have to do is select UserCell by putting the caret at the end or beginning of UserCell and using ⌥ ⇧ + or to select just that word (or double click the word), then: + . That’s it! Just one double quote is all you need as Xcode is smart enough to figure out the rest. If you needed () {} '' "" [], it would do the same thing—you only use the left one. And there’s only one selection for double or single quotes, obviously.

Here Are The Basics (Just in case)

  • Cut: ⌘ X

  • Copy: ⌘ C

  • Paste: ⌘ V

  • Undo: ⌘ Z

  • Redo: ⇧ ⌘ Z

Editing/Writing/Selecting On Multiple Lines

If you want to edit multiple lines, you can do this pretty easily.

For lines that are consecutive:

  • While holding , click and drag as if you're drawing a box around the text you want to select. This can be for as many lines as you want to cover. This can be a little finicky and you may (in Xcode) see a ? or crosshairs, but just keep selecting. Make sure you let go from the mouse before you let go of the option key to ensure the selection stays selected. Hit , or esc to cancel the selection. When you do that, you will see a bunch of carets (one for every line) show up.

  • To end up with carets that are ready to edit, you must drag in a straight line. Once you have the lines selected, make your edits. This can be speed things up nicely!

  • Placing a caret on multiple consecutive lines isn't only done by using your mouse or trackpad. You can just hold ⌃ ⇧ + or .

  • You may be asking yourself, what if I want to edit multiple lines that aren’t consecutive? Well, there’s a way! To do this, hold ⌃ ⇧ and just click where you want to place your carets. If you've accidentally placed a caret where you don't want it, just click the caret that you want to go away and it'll disappear. When you're done, hit esc and you'll go back to a single caret again.

More on Selection...but with a cursor 😬

I'm a HUGE keyboard shortcut guy, but for some things, the cursor is just quicker, period. So let's turn 😬 to 😉!

There are times when it's a lot quicker to work with the mouse or trackpad, especially when you combine the use of the keyboard. For instance, you're swapping out a property with another and there are a lot of places where the swap needs to happen, you'll want to combine these next tips together with other simple commands like copy, cut, or paste (as mentioned above in the basics section).

  • Select Word: Double-click

  • Select the whole line: Triple-click

  • Select multiple words by word (not character):
    Most people know that you can click and drag to select as much text as you want. This is actually selecting by character. Try this instead — double-click to select a word (don't let go), then drag. You'll see that your selection is changing by word now, not by character. I'd say most of the time, this is what I want. It's quicker and accurate!

  • Select by pairs:

    • Did you know you can select a whole string by double-clicking only one of the quotes?

    • Similar to the bullet point above, this also works for square brackets, parenthesis, and curly braces. Using this, you can easily select whole arrays, and closure blocks for example.

  • Select by line: Triple-click without letting go after the third click, then start dragging up or down.

Bonus Tip:
In Chromium based browsers, if you hold down the option key, you will be allowed to select the text of a link like you could with any other text!

Change All In Scope

This is a great one. Say you’ve written out your function but later realize that there’s a typo. It can be a pain to change all instances of it in that scope manually.

I think you’ll find it much easier to do it this way:

Just make sure your caret is on one of the locations of the name you want to change (caret can be at the beginning, middle, end of the name, or the name can be selected). Then press: ^ ⌘ E. You’ll see it highlights all the other instances of that name inside the current scope ready to be changed. As you make your changes, you’ll see the other occurrences update live. Once you’re done, just smash that return key and that’s it!

Note: If you need to edit instances that are scattered throughout different scopes or files, you would need to use the "Refactor/Rename" method. Right-click the name you want to change, select "Refactor", then "Rename".

The key command in this section is limited to the current scope you're in.


Navigating Xcode

For good measure, here are some quick ways to get around Xcode.

Quick Open

I can’t express enough how useful this is. If there’s anything you need to find and jump to, you can do this easily and fast by using: ⇧ ⌘ O (that's the letter "O"). This works on function signatures, class names, etc. Try it for yourself!

Bonus Tip 1:

If you replace the letter "O" with the number 0 in the command above, the Xcode documentation will open up instead!

Bonus Tip 2:

If you add a colon and then a number after your search query inside the quick open field, Xcode will not only jump to that file, but it will place you at that line number. For example, my query might look like: "NetworkManager.swift:82". That will take me to the NetworkManager file AND place me at line 82.

Project Navigator (Left Sidebar)

  • Project: ⌘ 1

  • Source Control: ⌘ 2

  • Symbol Navigator: ⌘ 3

  • Find Navigator: ⌘ 4 or ⇧ ⌘ F

  • Issue Navigator: ⌘ 5

  • Test Navigator: ⌘ 6

  • Debug Navigator: ⌘ 7

  • Breakpoint Navigator: ⌘ 8

  • Report Navigator: ⌘ 9

Jump Current File

Sometimes you may be in a file that you navigated to from another file or from jumping around via tabs, etc. If I'm in a file, chances are I'll need to reference files that go along with the file I'm currently in. But that file might not be highlighted in the left sidebar of Xcode/Project Navigator. To get the Project Navigator to jump to and highlight the current file you're in you can do so by pressing ⇧ ⌘ J. I remember this by thinking of J for "Jump".

Back & Forward in History

  • To go back in history: ⌘ ⌃ ←

  • To go forward in history: ⌘ ⌃ →

Find and Jump to Next/Previous Occurrence

I’m adding this tip cause a friend showed me this one and I think it’s good to know. If you want to quickly find a name in your code and you want to jump through the occurrences: ⌘ F to bring up the Find view where you can enter the text you're looking for. If there are any results, you can jump to the next result via ⌘ G or the previous result via ⇧ ⌘ G.

Quickly Navigating Within a File

  • ⌃ 6 will show you all the symbols in the current file where you can arrow to the one you want to jump to, or immediately start typing to narrow down the list.

  • ⌘ L will bring up a quick jump field similar to the quick open, but instead you enter the line number you want to jump to. Remember this easily by associating "L" with "line".

  • Lastly, let's say the left side bar is focused instead of the file and you don't wanna reach for your mouse, you can press ⌘ J which will highlight the editor. Just press return and the editor will now be focused! Also, if you have multiple editors going in the same window, just use the arrow keys to move the highlight to the editor you want to focus and then press return.


Well, that’s about it for now…

You’ll have to get yourself into the habit of using these over time, but once you do, it will save you a lot of time because your workflow will be much, much faster. I hope you get some good use out of this and I’d love to hear about how or if this helped you. If you have questions or spot a mistake, I’ll do my best to respond and edit or clarify. Also, I’m sure there are some good ones that I missed. Feel free to share the ones you know that I haven’t covered.

Also, the best way to drill this in is any time you catch yourself doing something the old way you're used to and do habitually, undo what you did and use the keyboard shortcut instead. For example, if I clicked the back arrow at the top in Xcode to go back to file I was previously in, I would actually go forward again just so I can go back using ⌘ ⌃ ←. This is how I built all of these shortcuts into muscle memory. Yes, that's a pain in the ass...at first! Then it's lovely when you don't even have to think about it, and now you're just doing something because it's most efficient.

Here are some links to take you further:

Thanks for reading and happy coding!