Search code examples
xcode

How to do block selection in XCode?


I have the this code:

selectedUser.firstName
selectedUser.lastName
selectedUser.age
selectedUser.foneNumber

I want to select the block (4 lines simultaneous)

selectedUser
selectedUser
selectedUser
selectedUser

and then replace from selectedUser to user (expected result)

user.firstName
user.lastName
user.age
user.foneNumber

How to do block selection (column selection) in XCode?


Solution

  • In addition to shift-control-clicking the individual occurrences, you can hold option key while selecting the range:

    option-select

    Here I option-selected the selectedU portion and then pressed u to replace that with the letter “u” on all lines. From there, it proceeds much as matt outlined (+1).

    But, bottom-line, I shift-control-click when I want individual, multiple cursors, but they might not vertically line up or might not be contiguous. I use option-select when they do line up nicely, as in your example, saving me a few individual clicks.

    Paul Hudson has a video on multiple cursors. That’s #32 on his Xcode tips and tricks.


    Your revised question amounts to a renaming a property or variable. That suggests two other options, too:

    1. A slightly broader “rename” of a property: select any occurrence of the property name and press control-command-e. Then edit the current occurrence and they’ll all change. Hit return to save the change.

      control-command-e

      Note that renamed the property as well as the four occurrences in that function.

    2. If you want the renaming to scan your whole project for occurrences to rename, go to Xcode “Editor” » “Refactor” » “Rename”:

      refactor-rename

      This is #14 in Paul’s Xcode tips series.