Check out a free preview of the full Visual Studio Code course

The "Selection" Lesson is part of the full, Visual Studio Code course featured in this preview video. Here's what you'd learn in this lesson:

Mike demonstrates the use advanced code selection features such as being able to select the next occurrence or all occurrences of the current selection. Mike takes questions from students.

Preview
Close

Transcript from the "Selection" Lesson

[00:00:00]
>> Mike North: So let's talk about selection a little bit. Cmd+A, select all for most things. This is true, if we were to just go to a browser here and hit Cmd+A, we actually end up selecting everything. So this is not a code thing, this is the way your OS works.

[00:00:15]
Select next occurence, so we've already looked at that, for me, it's Cmd+D. You can edit this file locally and put your key binding here so you remember it later. That'll be in this selection menu right here, Select Next Occurrence. Previous Occurence, I find that like I can spot the first one and then find it from there.

[00:00:41]
And keep in mind that when you say select next occurrence, it'll actually wrap around to the beginning of the file. Eventually if you just spam Cmd+D a few times, you will catch everything that has that name. Select all occurrences is pretty slick and for me, that key binding, it's at the end, it's Ctrl+Cmd+G.

[00:01:00]
So let's see if this this actually works, if I've got groceryItem here. Ctrl+Cmd+G, now I've got all groceryItems in my whole file selected. Not just that, but I've got a cursor at the end of each, so if there was a spelling error or something, I'd be in a great position to fix it and then save.

[00:01:22]
But I've got cursors all over the place here.
>> Mike North: So this, when we start talking about key bindings, sometimes you find that there's a really useful tool. Like maybe you discover that, okay, Cmd+D, finding the next occurrence. That's great, but really, you just select all is what you want most of the time.

[00:01:41]
You could always remap that, right? And I'm gonna suggest you only pick four or five of these things at least to start. Where you say, this looks really promising, but then do it gradually and incrementally. Developers like to jump in and tweak everything, tune their editor, and try to get used to everything at once.

[00:02:01]
And it is tough to retain all that, yes?
>> off screen: If you're using the at next occurrence, and you go one too many, say you had ten instances and you only wanted to select seven or eight of them, is there a way to undo?
>> Mike North: Yep, see, undo
>> off screen: How, which-

[00:02:20]
>> Mike North: That's Cmd+U.
>> off screen: Cmd+U?
>> Mike North: Cmd+U, and that, by the way, that is the same. So anyone in here used multi-cursor support at all, just clicking in a couple of different places, where you hold down Alt and click. And has anyone ever, when you're trying to edit six items, when you get to that sixth item, you're like, please let me land in the right spot here.

[00:02:41]
If you're one character off, you've created more work for yourself than you had a hope of saving. But you can just hit Cmd+U and it'll unwind those cursors. So you could go click, click, click, click and then Cmd+U, think of it as a little mini undo redo stack.

[00:02:59]
Right, undo redo does not apply to this, but it's got its own little mini sequence of operations. And it'll keep track of these things, like these selections here, and you can just hit Cmd+U and back it off.
>> off screen: Well I think you can Alt-click on it, as well, can't you, if you wanna pick ones out of the middle or something?

[00:03:18]
>> Mike North: Let's see, can I Alt click on this and remove it, I can. But that's dangerous territory, man.
>> off screen: [LAUGH]
>> Mike North: And for selection, yeah, I guess that could work. Let's see if it works with this, though.
>> off screen: If you wanna just remove one from the middle or something, you don't wanna edit one in the middle, you can just Alt-click that.

[00:03:38]
>> Mike North: Yeah, that's a good tip.
>> off screen: I did not know that, that's awesome.
>> Mike North: I like that, so that's like a random access way of getting rid of a cursor, in addition to the undo redo stuff. Cool stuff, I learned that just now, thank you for the tip.

[00:03:55]
>> Mike North: So fill in you key bindings as you go. This is really worth it, especially if you've got a variable that you're using in multiple different places. And now you find that you've collided with this other thing. Or in a code review, you bring your friend over, you start pair programming.

[00:04:10]
They say, I don't understand the name of that variable. It's not descriptive enough, time to go rename it. We'll look at a better way to rename in a moment.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now