Join Kevin Yank for an in-depth discussion in this video Using multiple selections to edit multiple lines simultaneously, part of Up and Running with Sublime Text 2.
Probably the single most whiz bang feature of Sublime Text 2 and the one that attracts the most new users to this editor is Multiple Selection. But Multiple Selection is also one of the hardest features to use in this editor, because it really isn't obvious how the editors supposed to behave with a multiple selections going on at once, so let me show you how it works. I am going to scroll down in index.htm to this unordered list that contains a bit of a markup issue there is inconsistent spacing following these closing span tags, and we like to fix that up by adding the spaces that are needed on these bottom lines.
The easiest way to do multiple selection is to start with a single selection, or in this case a single cursor, and then hold down the Ctrl or Command key to add additional cursors or selections, so you can drag out a selection, or you can just click to create a cursor. And I now have six different cursors in my document at once. You can see it says 6 selected regions in the status bar, and now anything I do--whether it's typing or issuing commands-- those will affect all of the cursors at once, so I can just hit Space to insert a space at all of those spots.
Now that was pretty easy. It was also not especially fast or efficient, I might as well have just inserted each of those spaces one at a time for the amount of time it took me to go through and click each of those spots to create a cursor. So, the easiest way to use Multiple Selection is probably the one that is the least worthwhile. Just, by the way, you can collapse a multiple selection back to a single selection by hitting Escape. A better way to do Multiple Selection is with the Add Previous or Next line commands, you can find these in the Select menu here Add Previous Line and Add Next Line.
Now the thing is the keyboard shortcuts for these on the Mac are currently assigned to Mission Control. So if we want to use this feature, you need to go into your System Preferences, and under Keyboard go to the Keyboard Shortcuts tab, make sure Mission Control is selected and untick or uncheck the Mission Control and Application windows shortcuts. Alternatively, you can assign new shortcuts to these, but simply disabling them for now is all we need to do. This frees up the Ctrl+Up and Ctrl+Down shortcuts for use by Sublime Text 2, so let's get back to our multiple selection.
On Windows the keyboard chart at his Ctrl+Alt+Up and Down, on Mac its Shift+Ctrl+Up and Down, and it lets us add a cursor above or below the current line. So if I do Shift+Ctrl on my Mac down, I'll create a new cursor on the line below the previous selection. Now this would work fine except for word wrapping, you'll see if I do Shift+Ctrl+Down again, I get my cursor over here, when where I wanted it to go was down here on line 48. Line 47 is wrapping down to here, and that's getting in the way of my edits, so we can fix this by going to View turning off Word Wrap, and I'll start over with a single selection here.
Now I can do Ctrl+Alt+Down Arrow or Shift+Ctrl+Down Arrow on Mac to create my six different cursors and then hit to make the edit on all of the lines. Great this is way more efficient and clicking to create each of those cursors, but I had to disable some system shortcuts on the Mac, and I had to disable Word Wrap which I actually quite like the rest of the time during my editing, so again. I don't think this is a particularly efficient way of using multiple selection.
So how should we do it? Well, by splitting a normal selection into multiple lines, this is the way I usually like to do multiple selection, so let me go back to the way this file was. And I start by saying, okay, I want to make this edit on all of these lines, and I'll just select them all like I normally would. And now the trick is to split this single selection that starts here and ends here into multiple selections one per line, there's a command to do this it's right at the top of the Selection menu Split into Lines, it's Ctrl+Shift+L or Shift+Command+L on the Mac.
When you do that. you can see that single selection that I had previously is now six separate selections, one for each line. You can see the cursor is blinking away at the end of each line. Now the line break that was at the end of each line is no longer included in those selections. So now that I have a selection for each line, I usually will do one of two things which is move my cursor to the end of the line or to the beginning of the line, I can hit the right arrow to move my curses to the end of the line. In this case, though, I want to press the left arrow to move all my cursors to the start of each line, because each line begins with a very consistent structure.
So I can now hold down the Option key and move all my cursors over to the end of each Span tag and then hit Spacebar so that might seem a complex way of working. But once you get into that headspace of create a big selection, split the selection into lines, and then edit all of the lines at once, this becomes a really efficient way of working in many cases, and you'll notice my Word Wrap didn't get in the way of anything in this case. So that's three different ways of achieving the same edit using multiple selection, each one a bit more efficient than the last.
- Opening, viewing, and editing files
- Adding custom themes and color schemes
- Performing different types of find and replace operations
- Editing multiple lines simultaneously
- Automating tasks with macros and snippets
- Working with add-ons like Package Control, Line Endings, and Hyperlink Helper