Have you ever used Multiple Cursors in Xcode? 🤨
Hi 👋
Last week I was at Do iOS in Amsterdam, for my last conference this year!
The conference was really nice and I got the opportunity to talk about Swift macros:
(and there’s a good chance that I’ll re-use the content of this talk in a livestream later this year 🤫)
But before we go into the topic of this email, I have a big thank you to my sponsor this week: Bitrise 🤖
Advertisement
Sponsors like Bitrise really help me grow my content creation, so if you have time please make sure to check out their survey: it’s a direct support to my content creation ☺️
Last week I’ve told you about the advanced features of breakpoints in Xcode, and this week I want us to stay on the topic of little-know features in Xcode!
This time we’re going to talk about multiple cursors and how they enable us to quickly edit repetitive pieces of our code.
Let’s consider this piece of code:
While each of these 4 lines has a different role, they all follow the same structure.
So there’s a big chance that if we need to edit one of these lines, we’ll also need to do the same modification on the 3 other ones.
And that’s when multiple cursors can really help us!
By holding down the keys CTRL ⌃ + SHIFT ⇧ and then using the directional arrows, we can create multiple edit cursors:
And once these multiple cursors are in place, we can start typing and editing all 4 lines at once:
Even better, if we hold the OPTION ⌥ key, we can then use the directional arrows to navigate in the code by going from one identifier to the next.
This enables us, for instance, to edit the arguments passed on each of the calls all at once:
But that’s not the only use case for multiple cursors!
Now let’s consider this second piece of code:
Imagine that we want to implement a custom CodingKeys
enum
inside this struct
.
Doing so would typically be a tedious task, because we’d need to create a case
in the enum
for each of the property in the struct
.
But through the use of multiple cursors, it’s possible to make this task much easier!
First, we can use multiple cursors on each of the properties, to copy the beginning of their declaration:
And then, still using multiple cursors, we can paste the code in the enum
and immediately edit it, to replace the keyword let
by the keyword case
:
From there, we can get back to using a regular cursor to make any additional change, like updating the coding key of a specific property:
That’s all for this email! I hope you’ve enjoyed discovering how multiple cursors work in Xcode!
Now that you know how to use this feature, I’m sure that you’ll find ways to use it in the days and weeks to come 😌
That’s all for this email, thanks for reading it!
If you’ve enjoyed it, feel free to forward it
to your friends and colleagues 🙌
I wish you an amazing week!
❤️