A cursor is a position indicator of where we are editing our code. Typically, one cursor is all we need.
Multi-cursor editing is a feature that allows you to place multiple cursors in your code and edit them simultaneously.
There are a few ways to enable multi-cursor editing.
To add a new cursor, hold Control and SHIFT then click.
⌃ – Control + ⇧ - SHIFT + Click
If you want to add multiple cursors at the same column, instead of wasting time aiming and clicking, you can use the arrow key to navigate. Hold ⌃ – Control and ⇧ - SHIFT then ↑ or ↓ to add cursors to the line above and below your current position. You can deselect a cursor by hit the arrow in the opposite direction.
⌃ – Control + ⇧ - SHIFT + ↑
⌃ – Control + ⇧ - SHIFT + ↓
If you don't want to hold multiple keys at the same time, this is the method with the least keys holding. To add multiple cursor, hold Option then drag your cursor around.
⌥ – Option + Drag
To remove a cursor, hold Control and SHIFT then click on the existing cursor. You can use this regardless of whatever method you add a cursor.
⌃ – Control + ⇧ - SHIFT + Click
To go back to single-cursor editing, just hit esc.
If you never see this trick before, you might wonder what the benefit of editing in multiple places at the same time is. There are countless ways to use this multi-cursor editing, which you might pick up along the way. I will show some of them to get you started. After you see these examples, you might come up with the one that suits your workflow.
Let's say you have a struct that you want to have a custom coding key. Define all the key for all property in the struct is a tiring process. Multi-cursor editing can help make the process easier.<1> First, I copy all the property in the struct. <2> Then, we paste them into the `CodingKeys` enum. <3> We use a multi-cursor to replace `let` with `case` and remove each property type. <4> After that, we exit multi-cursor mode and add a necessary change.
We add cursors before each parameter with Control + SHIFT + click then add a new line to expand our parameter into multiple lines.
Multi-cursor is one of my favorite features. I hope you find this trick useful and got it under your belt.
Read more article about Xcode, Workflow, or see all available topic
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.Become a patron Tweet Share
Getting the number of days between two dates in Swift
There are a few variations when dealing with counting days. You need to ask yourself some questions beforehand.
Scaling custom fonts automatically with Dynamic Type
Font is an essential part of an app. A good selection of font would make your app stand out from the crowd. But whatever fonts you choose, you have to make sure it doesn't lose its core function, readability. You might feel reluctant to use a custom font in the past because you might lose the benefit of dynamic type goodness that Apple provides with their system font. Since iOS 11, this is no longer the case. You can easily use your custom font dynamic type.