Sublime Text tips and tricks

I’ve tried many code editors during my developer career. And though I like how intelligent JetBrains products are, how lightweight and customisable Vim is, my weapon of choice is still Sublime Text.

Let me share the tricks involving Sublime Text I’ve collected.

Selection range tricks

Behaviour of “Expand selection to a word” feature, (Ctrl+D) depends on how you created the selection. If you put the cursor inside a word and press (Ctrl+D) it’ll select only the full word itself, but if you start with a selected letters range, even if it’s a whole word selected, it’ll select all the occurences of the letters sequence.

Sublime Text multiple ranges management

The same applies to Cmd+E, Cmd+G (“Use selection to find”, “Find Next”) and Ctrl+Cmd+G (Select all occurences) shortcuts.

Soft undo

“Soft undo” (Cmd+U) is a very nice Sublime Text feature. It’s similar to plain “Undo” command, but it also takes selection state into account. So you can cancel an accidental Cmd+D multiple selection keypress.

Sublime Text soft undo

By the way, if we talk about multiple ranges selection: you should also know how to skip a single occurrence: Cmd+K.

Sublime Text skip next occurence

The last shortcut in this section I’d recommend you to learn is “select all occurrences” (Ctrl+Cmd+G). Again, depending on whether you select a whole word or its parts only, it’ll behave differently.

Sublime Text select all occurences

Split into lines

“Split into lines” (Cmd+Shift+L) is a very powerful command. It allows you to create multiple range selection for each line from a bigger selection.

Sublime text: split selection into lines

Select the range you would to turn into multiple line selection ranges. Press Cmd+Shift+L in order to invoke the command. You can navigate the selections word by word by pressing Option+Left arrow or Option+Right arrow, go to the beginning of the line (Cmd+Left arrow) or to the end of the line (Cmd + Right arrow).

Use “join lines” command (Cmd+J) in order to join the lines together.

Keyboard Sidebar Navigation

For those who prefers to use keyboard: good news! You can use arrows keys to browse files in your Sublime Text project sidebar. To enter this mode use Ctrl+0 (zero) shortcut.

Overscroll in macOS

After I’ve migrated to macOS from Linux I missed Sublime Text “overscroll” feature. What I mean is that even when you’re close to the ending of the file you still can scroll any part of the code to the center of the screen.

But it appears that the functionality is available in macOS too, you just have to activate it manually.

Use “scroll_past_end”: true configuration flag to activate this feature.

Multiple line regular expressions

I have to admit that I didn’t use this feature very often, but sometimes you need to match multiple lines with your regular expression. Imagine you want to match two first lines in the following text (from “Mary” to “snow”):

Mary had a little lamb,
Its fleece was white as snow,
And everywhere that Mary went
The lamb was sure to go.

Obviously, expression ‘mary.*snow’ won’t match the lines, because by default Sublime Text applies its regular expressions in a single line mode.

What I did before is I replaced dots in my regex by presence of a symbol and its absence, combined by OR operator. Let’s use letter “a” as a symbol (any other would work). So we’re replacing the dot in our regex by “(a|[^a])”, meaning “a, or not a”. Final expression in that case looks like ‘mary(a|[^a])*snow’.

It’s a nice workaround, but it appears that the functionality to achieve that is built-in into Sublime Text. Prefix your expression with “(?s)” and your regexp will be treated as a multiline one.

So, in Mary example you can put “(?s)mary.*snow” in your search box to match 2 first lines.