A Sublime Text selection primer
Text selection shortcuts make editing code (and even prose) much faster. Learning the selection shortcuts in any editor you choose to work in is paramount to saving time and effort. I currently do most of my coding in Sublime Text 3, so I’ve been practicing some of my lesser-used shortcuts. Here’s a basic cheat sheet.
Forewarning: I customize so many aspects of my editor that I may have forgotten that some of these aren’t defaults. If you run into conflicts or problems, let me know and I’ll either update this post or show how I modified my config to get the shortcuts working.
Text Selection
-
⌘D
: Expand to wordNote that you can define word separators in preferences, so underscores and dots can be included in a “word” selection.
-
⌘L
: Expand to lineThis is different from doing a
⌘←
,⌘⇧→
in Cocoa text fields. It selects what OS X refers to as the current “paragraph,” meaning the first character to the next line break, instead of just the current screen line. -
⌘⇧A
: Expand to tagHandy in XML formats such as HTML and PLIST files. It selects the text inside the current tag pair, and pressing again selects the tags (open and closing) surrounding it.
-
⌘⇧J
: Expand to indentationIf your code is properly indented, this command is really handy for grabbing everything inside, say, an
if
block. -
⌘⇧␣
(Space): Expand to scopeExpand to scope is a great way to create selections, and it can progressively increase the selection with repeated presses. Select the text inside a quoted string, then the next time you press it, it will add the quotes themselves. Press again to select to the nearest surrounding brackets, and again to include the brackets themselves, and so on.
Combine this with other selection commands and you can do things like select the current scope, and then select everything at the same indentation within the current block with just a couple of keystrokes.
-
^⇧M
: Select to bracketsThis is another extremely handy selection technique. It’s just like “Expand to scope,” but skips directly outward to the nearest square or curly brackets. On the first press it will select inside the nearest brackets, pressing again will select the brackets. Subsequent presses will select the next pair outward, including the brackets themselves.
-
BONUS:
⌘U
will undo movements and selections, and⌘⇧U
will redo them.Known as “soft undo,” use this if you accidentally lose a selection and want to get it back.
Multiple selections
⌘
-click/drag: add cursor/selection^⇧↑
/^⇧↓
: add previous/next lines as multiple cursors, then use⌘←/→
to select sections/lines.-
⌘⇧L
: break a block selection into linesIf you want to edit multiple lines at once, you can select the block of text and hit this shortcut to turn each line into a selection in a multi-selection. Then move the cursor and hold shift as needed to create selections within each line. When you type, you’ll affect all cursor locations and selections.
^⌘G
to select all instances of the word currently under the cursor-
Search (
⌘F
or⌘I
), then use^⌘G
to turn results into multiple selectionsIf you want to act on multiple selections based on a search, use this to turn every instance of the result into a selection. It’s faster than dealing with Search and Replace. You can also grab a package from Package Control that will allow you to quickly do the same with regular expression searches.
My keybindings
I override a few keybindings, but just want to mention a couple that I think are handy.
-
⌘⌥↑
: Expand selection to scope.I added this because it matches similar commands in some of my other editors.
-
⌘⇧M
: Expand selection to bracketsI added this just for continuity with the other selection shortcuts.
Add-ons
Open Package Control and search for “select.” There are a lot of packages available for extending selection options. Some standouts for me:
- SuperSelect makes it easy to add search matches to the current selection one at time, selectively.
- Select Quoted
- Expand selection to function (JavaScript) lets you assign a key combination to select a full JavaScript
function(){}
definition in various forms.
I’ll probably get around to making a Cheaters sheet and Dash docset for these soon, but right now it’s just a note in nvALT for me.