106

What is 'Multi-cursor editing' in Xcode 10 editor. (more information about the same is mentioned in release notes but unable to understand.)

How exactly does it work?

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 3
    its used for allowing to quickly edit multiple ranges of code at once – Anbu.Karthik Jun 05 '18 at 09:48
  • 2
    Good question actually. I found [this twitter post](https://twitter.com/dmartincy/status/988094014804160514) on this matter that seems to talk about some third party plug-in / workaround. I'm not sure how to enter that editing mode, however. – LinusGeffarth Jun 05 '18 at 09:49
  • 3
    See https://developer.apple.com/videos/play/wwdc2018/102/?time=2518 for a quick demo – Hamish Jun 05 '18 at 11:07

5 Answers5

212

To edit multiple instances of text within different sections of a document, you can use multi-cursor editing. This allows multiple cursors to be placed in different spots so text can be added, modified, or deleted.

its the name of Source Editor, for reference purpose I taken the answer from whats-new-in-xcode10 and Sample link 1 and Sample link 2

The Xcode 10 Source Editor now supports multi-cursor editing allowing you to quickly edit multiple ranges of code at once.

  • shift + control + click
  • shift + control +
  • shift + control +
  • option + drag

With a source control-enabled project the source editor displays changes made by a developer in the gutter and shows changes made by other developers that haven’t yet been pulled into the project

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Can you clarify what exactly the up arrow, and pretty much what every other symbol means except the command key? my retina macbook does not show those symbols, for example the option key just says "option". – MarcusJ Jun 11 '18 at 07:16
  • 1
    @MarcusJ - I modified the answer, its easy to understand, for sample you get [here](https://qiita.com/okuderap/items/3289265e4645a29b6808) – Anbu.Karthik Jun 11 '18 at 07:46
  • Is it possible to change the key binding for this ? I'd rather not use the control key. – DogCoffee Sep 16 '18 at 01:47
  • 2
    I wish they would have just done command + click like Sublime. – Eric Mentele Sep 19 '18 at 17:21
  • I was hoping to see something like Sublime or Atom's `Cmd + D` multi-select/multi-cursor. I don't know why Xcode has to be so spare with helpful features. – Dan Oct 08 '18 at 17:33
  • 5
    @Dan just change the keyboard shortcut for "Select Next Occurrence" to `Cmd + D` in preferences :) – RyanM Oct 13 '18 at 21:04
  • 1
    To select Multiple lines simply use "Shift + Ctrl + Drag" in Xcode10, which could achieve by "Cmd + Drag" earlier ✌️✌️✌️✌️ – Mohit G. Oct 16 '18 at 11:56
  • I searched all key bindings for "multi", "caret", "cursor", "input", "add" but never found this feature. Finally resorted to DDG, and then to Google. Great discoverability. – Andreas Nov 10 '18 at 22:08
  • For me multiline drag is done with `Ctrl` + `Option` – Tum Jan 07 '19 at 11:38
  • 1
    @Dan, Check out [my answer](https://stackoverflow.com/a/56545206/865175). – Iulian Onofrei Jun 11 '19 at 13:48
34

The best way to use it is by using the Select Next Occurrence command from the Find menu.

Its default keyboard shortcut is alt + cmd + e, but you could set it to cmd + d to mimic Sublime Text's behavior.

This way, you can edit code lines that are different, whereas the solutions in the other answers only allow you to edit similar lines.

For example, if you have this code:

NSString *myStringg = @"stringg";
// print the stringg
NSLog(@"Here is my stringg: %@", myStringg);

you simply:

  1. manually select the first Stringg occurrence from the first line using the cursor
  2. hit the Select Next Occurrence's keyboard shortcut 4 times
  3. hit the right arrow key
  4. hit backspace

and you'll have:

NSString *myString = @"string";
// print the string
NSLog(@"Here is my string: %@", myString);
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
10

Usages

(1) Edit CodingKey

using Option ⌥ + drag

demo-1

(2) Edit method parameter indentation

using Shift ⇧ + Control ⌃ + click

demo-2

Related Defaults

$ defaults write com.apple.dt.Xcode PegasusMultipleCursorsEnabled -bool true

Reference

  1. https://sarunw.com/posts/multi-cursor-editing-in-xcode/
  2. https://github.com/ctreffs/xcode-defaults
  3. Mac keyboard shortcuts https://support.apple.com/en-us/HT201236
George
  • 3,384
  • 5
  • 40
  • 64
6

Shift + Ctrl + click when you wish to edit same text in file for multiple times e.g

    option_A.isEnabled = false
    option_B.isEnabled = false
    option_C.isEnabled = false
    option_D.isEnabled = false

in this i have to put true on all four lines then it should be better for to put true at once by using shift + control + click rather than edit each line

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
2

There is also a keyboard shortcut available for those like me who use cmd + Shift + L in Sublime.

It's called Selection - Split Selection By Lines. I set it to cmd + Shift + L but I had to set the Show Library shortcut to something else random that I don't use to resolve the conflict.

Sonastra
  • 182
  • 1
  • 9