Lessons: 34Length: 2.5 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Multiple Cursors and Incremental Search

So for my first real tip, I wanna show you the thing that really brought me over to Sublime Text Two instantly and that was the concept of multiple cursors, which is something I'd never been exposed to before. So consider this in your current editor, assuming you're not using Sublime. Let's say, right here, this paths variable has changed and you need to update it in each case. So think about it, how would you have done it. Well, let's say, the new variable is p. Well, would you have just gone to each one? Well, it's possible that might have been what you would have done, but that takes a long time. Next, you might have simply done something like a search and replace. Now in Sublime, I can hit Cmd+Option+F or you can go to Find > Replace. And if you're a Windows user, this would be a different command of course, and we'll go over the commands shortly. So we can bring that up and we can look for paths and replace it with p and that would work, but still that takes too long. I'll show you a way to use multiple cursors to get this done. If I were to hit Cmd+D, notice that it will instantly select the next occurrence of that sequence. Now a quick note before we move on, I am using the Mac shortcuts. Honestly, because I don't use Windows and I don't have those memorized. And also, you guys don't wanna hear me saying lots of commands for every OS. So I'm going to recommend if you are a Windows user. Go to sublimetext.com/docs/2 and for every feature I'm going to be going over, you can see what your counterpart would be. So in this case, we're going to multiple selection. And notice Cmd+D for Quick Add Next. On Windows, it would be Ctrl+D. So often, the only difference will be, you press Ctrl on Windows, I press Cmd on Mac, but sometimes it'll be different. So when in doubt, refer to the documentation. All right. Let's continue on. Now I can continue pressing Cmd+D and it will select every additional occurrence of that sequence. So you can see how helpful that is. Now what's coolest here though, is that I now have multiple cursors. Watch what happens if I type new letters, I'm updating that in every single location, which is tremendously powerful. So you can imagine if I needed to change this and I were in a actual project, I would select it and I could press Cmd+D just a handful of times, change it, and I'm done. And I did that in two seconds, but we can make this even faster. Before, I was selecting the entire word, but you can also just place the cursor within the word. And when I press Cmd+D the first time, that's going to select the word and that will be like, normal selection. So in this case, it's not selecting the dollar sign, because it doesn't think that's part of the word. But if I were in for example, application, Cmd+D, now I've selected the word and I can change it. So that's good for just simple changes. I'm in application, change it to app, Cmd+D. So if I come back to paths, I hit Cmd+D. Once again, without selecting it, I can select all of those words and I'm done. Now here's the final thing, though. Cmd+D will select the next occurrence of the word. However, Ctrl+Cmd+G will select every occurrence of that word. And notice once again, I'm not selecting this word. I'm placing the cursor within it and pressing Ctrl+Cmd+G and now that's instantly selected the word and found every additional occurrence of that word too and now I hit p. So now rather than the old way when you did Search and Replace, now I'm simply placing the cursor, Ctrl+Cmd+G+P and I've updated every occurrence of that variable. Now those are the two core ways that I work with multiple cursors, but you can do column selection if you prefer that. So, if I hold down Option on the Mac and I select, you'll see that we are selecting in a column way and if you're a TextMate user, you're probably familiar with this. So I can come down here, change all of these and update it. And of course, in this case, that wouldn't be effective, but there will be situations when maybe column view is right for you. Now here's another way. So let me just add some gibberish right here. [BLANK_AUDIO] And I'll repeat that a few times. If I want to update these, there's a couple ways. I could as before, select it, Cmd+D a few times or select it, Cmd+Ctrl+G to select them. Or I can use the cursor, select these and hit Shift+Cmd+L and think L for lines, L. And now I can update those, so you can have Cmd+D, Ctrl+Cmd+G and Ctrl+Cmd+L. Now, if that seems difficult to memorize, it's really not. Because it's such a helpful feature, it'll become muscle memory. I don't even think about it at this point. So that does it for this lesson. If you wanna play around with it, maybe considering these lines right here. Think to yourself, how could I make the t at the beginning of each one of these lines uppercase? Well, let's use a few different methods to knock this one together. To start, I can put the cursor within one of the words, hit Ctrl+Cmd+G and try to update it. But as you can see here, the problem with Ctrl+Cmd+G is that it selects every single occurrence of the word. But frequently, as is in this case, we don't want to select every occurrence. We just want to select all of the occurrences within this area. So in that case, that's when Cmd+D is a good choice. Cmd+D a few times and change it to The. Here's the next choice, column selection. So I will hold down Option and now I'm going to select all of those occurrences and change them to T. And then finally, I can select everything, Ctrl+Cmd+L. Go the beginning of the line with Cmd+left and now update that. So before I finish off for this lesson, let me show you one last tip. Let's say the cursor is right here, maybe within environments, but you notice, hey, I need to update this to a capital T for the first word. So you could take the mouse, click on it, hit Cmd+D a few times. Or another way is to use instant search. So normally with search, you probably hit Ctrl or Cmd+F to bring up that screen. And you would search for it, but then you have to hit Escape to hide the screen and then you would continue on. And I know these seem like very short actions, but they add up over the course of the day. And the developers who are able to be the most fast know how to trim a couple seconds from every single operations that they use. So if I were here and assuming I didn't have vintage mode turned on, I can hit Cmd+I for Incremental search or got to Find > Incremental Find. So now I type the word and notice that the next occurrence of it will immediately show up. So then I hit Enter and I'm already there, changed. So once again, my cursor is up here, I need to change that, so I would do Cmd+I. Find the next occurrence of the and then Cmd+D a few times and change that to The. And that does it for this lesson. Play around with these multiple cursors and commits those keyboard shortcuts to memory.

Back to the top