Join Kevin Yank for an in-depth discussion in this video Expanding selections of code, part of Learning Sublime Text 3.
- [Instructor] So many editing tasks start with a selection, so if you can get your hand off the mouse and keep it on the keyboard as you create text selections you're going to be more efficient overall. Let's start with a simple one. The option key or the alt key on Windows. If you hold option or alt, then using the arrow keys will move you through text one word at a time instead of one letter at a time. And this works in code as well. It's a much quicker way of getting around when you're just browsing for something. And if you simply add the shift key to this you can build a selection as you go.
It surprises me just how many people don't know and use this because it works operating system wide. Looking at things that just work in Sublime Text though. There's a whole bunch of commands here on the selection menu. These expand selection commands are really useful for building up selections. Let me show you what each of them does. First of all, if you've got your keyboard cursor on a word you can instantly select that word with command, D or control, D on Windows. You can select the whole line with command, L or control, L, L for line, that's pretty obvious.
There's this select paragraph command and it's not obvious at first what it's good for, but if your code is structured like this with chunks of code separated by blank lines above and below them, you can select the whole chunk of code using expand selection to paragraph. And if you write code like this a lot like I do, it might be worth finding a keyboard shortcut that you can assign to this command. Going back to HTML code, you can select by tag structure. So if I put my keyboard cursor here in this span tag, then I can invoke the expand selection to tag command with shift, command, A or control, shift, A on Windows.
Control, shift, M. And again, if you hit it again it'll select the brackets themselves, the parents and so on. You can select by indentation as well if the brackets aren't doing it for you. You can go to an indented line and select by indentation, that's shift, command, J or control, shift, J on Windows. So you hit that and it selects all of the lines that are at that same indentation level, there's just one in this case. You hit it again and it selects the parent indentation level and so on. Again, a very quick way to select the contents of a function or a while loop or anything like that.
Finally, there's select by scope which is kind of of an, if I can see it I can select it feature. Because scope is what Sublime Text uses internally to calculate the syntax highlighting that's applied to your code. So if you can see a piece of code has syntax highlighting applied to it, that is it appears in a different color, you can assume that it is a scope in the code and you can do shift, command, space or control, shift, space on Windows to select it. And this is especially good for squirrelly things like this character entity that would be hard to select any other way.
Shift, command, space instantly selects it for me there and I can see that's a possibility because it's in a different color. So there's all the different ways you can expand selections in Sublime Text 3. And depending on what kind of code you edit you'll want to train some or all of these into your fingers.
Instructor Kevin Yank takes students through the basic, not-so-basic, and downright hidden features of the editor, demonstrating how to use each tool and command to become more productive. Find out how to find and replace sections of code, bookmark your position, leverage helpful shortcuts, edit multiple lines of code simultaneously, and automate some of your work with autocompletion, snippets, and macros. Plus, learn how to tweak the appearance and configuration to make Sublime Text work best for you.
- Juggling multiple files
- Managing files and settings across multiple projects
- Finding and replacing text
- Customizing the look and feel
- Using multiple selections to edit multiple lines and large amounts of text
- Autocompleting code
- Using snippets and macros
- Extending Sublime Text with packages