Skip to content

Swift Refactoring #2: Refactor more quickly with multiple cursors

Don’t you hate it when you need to rename something in lots of places? AppCode has great refactoring tools to help us rename properties, classes and methods. But sometimes we might want to rename something that also appears in a comment, or we might have made a spelling mistake in more than one place, or we might want to change a value in multiple places.

Individually changing and manually typing each piece of text can be quite irritating, and slow. We can sometimes speed the process up by copying and pasting, but there’s another way. AppCode supports editing using multiple cursor simultaneously.

I tend to organise my unit tests into sections by the method that is being tested. In the example below I am testing a method called setFontStyleBuilder . Each test method is named  test_  followed by the name of the method, followed by a description of the behaviour.

When I refactor a method name I risk the MARK:  comment and the test method declaration becoming out of sync with the code. Isolating the method name inside the test method declaration is tricky and makes copy and pasting harder I would like so instead I use multiple cursors.

Use ⌥⇧ click  to add another cursor to your editor; make your changes; and then use  esc  to finish.

Use multiple cursors to change method names in comments and tests

Sometimes the things we want to change are on adjacent lines. In which case we can use turn on column mode  ⌘⇧8  and then holding shift   use the arrow keys to make a selection. Don’t forget to turn column mode off again when you’re finished using ⌘⇧8 .

Use column mode to edit mulitple lines simultaneously

Handy for when you’ve forgotten to make your properties private too.

Use column mode and multiple cursors to make all properties private in one go

You can even get crazy and copy and paste during your selection.

Copy and paste whilst using multiple cursorsHappy coding!

Subscribe now to hear more about AppCode tips and tricks

Published inAppCodeEssential tips you didn't know that will make you ditch XcodeiOSSwift refactoring with AppCode

Be First to Comment

Leave a Reply