
Lesson Description
The "Multiple Cursors" Lesson is part of the full, Become a VS Code Power User course featured in this preview video. Here's what you'd learn in this lesson:
Steve demonstrates different ways to use multiple cursors, such as holding down the option or alt key and clicking to create multiple cursors, using command/ctrl + D to select the next occurrence, and using regex with option/alt + enter to create multiple cursors on matching patterns.
Transcript from the "Multiple Cursors" Lesson
[00:00:00]
>> Steve Kinney: The other really powerful thing in VS Code that I use, honestly, every day, all the time, too often, is multiple cursors, right? Multiple cursors does what you think it does. There's a bunch of ways to use multiple cursors. One is Hold down the option or Alt key and start clicking.
[00:00:26]
Now, I have multiple cursors. You will learn something very quickly, though. Your aim is terrible, [LAUGH] right? So what do you do when your aim is terrible? So let's say I was hitting Alt. The nice part about my font being gigantic right now is my aim is great.
[00:00:50]
But on a normal font size, on a normal size monitor, with a trackball, my aim is terrible. So let's say we start with one here. I put another one and then whoops, I missed the @ symbol. All right, so command or I think Ctrl+u will remove the last one, right?
[00:01:10]
That's good, that's great. But ultimately, you're never going to click on all the things that you want, right? So let's say hypothetically, I wanted to take all of these function declarations and I wanted to turn them into arrow functions, right? I could do this.
>> Steve Kinney: And I can find the next one.
[00:01:39]
I'm not doing this. I can't live like this. The other option is you can select something, right? In this case export, but let's say export function and then hit Cmd+d and it will grab the next one. And you can hit Command D again and it will grab the next one.
[00:02:00]
Do that enough, and you can see it actually filled out the search thing, which we'll see in a second. And now I've got all of the export functions selected with multiple cursors. Now, I can do something like this. You'll notice that I have done this before. Const.
>> Steve Kinney: There we go.
[00:02:25]
>> Steve Kinney: And if we scroll down, my multiple cursors all worked on their given line, and I just changed all of these function declarations into arrow functions. Useful a lot of times we are cleaning up text, right? Or trying to find all your console logs, right? So delete them, right?
[00:02:46]
Could you've gotten really fancy with a Regex in the search and replace? Yes, you could have. Are you going to? No, it's better, it's easier to find one console log and slam on Command D until you have all of them and select the line and delete it. So that will work.
[00:03:05]
The other thing that you can do that you will know it when you need it, is, this is one of the things I've done from muscle memory so many times, I gotta think about it, is, so on a Mac, it's Cmd+Option+Arrow. You can just Start putting cursors up and down, right?
[00:03:23]
And grab them. That's nice if you've got a bunch of stuff in a given CSV or something along those lines. The one that I have never used in my life.
>> Steve Kinney: I literally don't even remember how to do it. Is there is some way to select a square?
[00:03:45]
I don't know why you'd want to. Maybe a marked, I don't know. I can't even come up with a hypothetical reason why you would do it. I have it in my notes, but literally, you could do it in TextMate too. I just have never wanted to do it.
[00:04:01]
Shift Option. I'm hitting if you don't hit the right key while you're talking, nothing works if you if you think shift option really hard and you hit control shift, it doesn't do the thing.
>> Steve Kinney: So you can see, I can grab an area with multiple cursors. Yes, that is the appropriate reaction of, why would you want to do that?
[00:04:27]
I don't know. I don't know why you would want to do that, but you can. I couldn't even remember how to do it, because I don't know why I would want to do it. I've never done it before my life. Maybe somebody could be like, I needed that and I provided a service.
[00:04:44]
Beyond that, I don't know why you would wanna do it. The other one that you can do it, I actually made this its own page in the notes. This was a month ago when I was gonna lose it. Which is.
>> Steve Kinney: Export const, and let's give it some kind of regex in this case.
[00:05:06]
We'll say.
>> Steve Kinney: If you hit hold option or Alt and hit Enter, you will get multiple cursors on everything that matches the regex. Right, so you don't even have to have the exact word. You can have roughly a pattern. If you hold option or alt while you hit enter, you will now get multiple cursors on everything that matches that regex.
[00:05:30]
Which is useful, let's say some of these had three arguments and I wanted to still match the entire line. I could get fancy with the regex and I could do it. When did I need this? Sometimes numbers have one digit and sometimes they have two digits. I needed both, all right.
[00:05:51]
That's the very esoteric use case that I had. But again, the idea that you can just have multiple cursors and it's a little bit of a dance that you get good at over time. Where I can refactor like large swaths of things just by kind of like being nimble with the multiple cursors, right?
[00:06:10]
And change especially if it is, again, a big JavaScript object with a lot of the same fields, right? You grab the one field. You Command D to your heart's content, grab all of them, and then you can theoretically trim the white space off the line, or wrap them all in an arrow function, or whatever you need to do.
[00:06:28]
Anything that you find yourself doing tediously, again, stop, pause, reflect, realize that there's probably an easier way to do it.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops