Tips & Tricks

Do several things at once with AppCode

Since version 3 AppCode includes an awesome feature – multiple cursors and selections, that allows you to do many deals at a time increasing your productivity! Let’s see how you can use it and when it’s a good case for this feature.

Multiple selections allow you to put a cursor in multiple locations in your file and edit them at once. This feature works with all languages supported by AppCode such as Objective-C, C, C++, HTML, JavaScript, CSS and more.

To add/remove a caret press ⌥⇧ and select the caret locations with the mouse. To restore single caret mode, use Esc. Now you can write code simultaneously in all these positions. And a good news for you – autocompletion and live templates will work in this case. For example, let us add some logging to the code with log live template:
mc_set_type

Actually a lot of editing actions are working perfectly in a multiple case, for example:

  • Move caret to line end (or with selection): ⌘→ (⇧⌘→);
  • Move caret to line start (or with selection): ⌘← (⇧⌘←);
  • Copy (⌘C) / Paste (⌘V) / Cut actions (⌘X);
  • Move caret to next word (or with selection): ⌥→ (⇧⌥→);
  • Move caret to previous word (or with selection): ⌥→ (⇧⌥→);
  • Complete statement (⇧⌘⏎);
  • Extend/shrink selection to the next logical block of code (⌥↑/⌥/↓);
  • Join lines (⇧⌃J);
  • and much more.

Autocompletion and live templates are also available, as we said before. The context is determined from the location of the last set cursor, resulting changes are applied to all cursors.

You can also easily change any repeating code fragments at once with placing cursor at any occurrence and then adding next occurrence of the current word to the selection by pressing ⌃G (to delete from the selection press ⇧⌃G) or select all occurrences at once by ⌃⌘G. For example, let us change all NSString constants’ postfixes in a piece of code below from Pref to PrefKey :
mc_occurrences

In the example above we can’t achieve the same result with Rename (⇧F6) refactoring. However there are situations when Refactoring actions are more appropriate than using multiple cursors. For example, if you are renaming variable occurrences we advise you to use Rename (⇧F6) refactoring because of two reasons:

  • not to break any usage you could miss while placing the cursor;
  • not to rename the usage from another scope by accident.

AppCode takes care of these situations so that you can rename safely with it.

Following the same reasons for the case when there are several equal NSString literals in a block of code, instead of using multiple cursors for changing the values try to use:

  • Extract Variable (⌥⌘V) refactoring or
  • Extract Constant (⌥⌘C) refactoring.

That will keep your code neat and safe.

We’d like to emphasize here that inspections and quick-fixes functionality are not available with multiple cursors. The reason here is that for applying quick-fixes in several places at once AppCode has a convenient batch-mode. Just call inspection by its name (⌥⇧⌘I) or use Code | Inspect Code for a desired scope, select a group of problems you’d like to fix in the resulted window and press ⌥⏎ to apply fixes:
inspection_batch_mode

We encourage you to try AppCode 3 and the awesome multiple cursors feature if you haven’t already, and do give us your feedback to help make it even more intelligent. To sum up let us show you one fantastic sample with multiple cursors:
Multiple_carets_inv_colors

Read more tips’n’tricks posts in our blog.