This chapter will guide us from Sublime's basic features to its most advanced ones, and explore techniques to use while editing code. We will also install two important plugins for most languages and master Sublime's Shortcuts Map.
In this chapter we will cover the following topics:
Search and Replace is one of the common actions for any text editor. Sublime Text has two main search features:
Before covering these topics, let's talk about the best tool available for searching text and especially, patterns; namely, Regular Expressions.
Regular Expressions can find complex patterns in text. To take full advantage of the Search and Replace features of Sublime, you should at least know the basics of Regular Expressions, also known as regex or regexp. Regular Expressions can be really annoying, painful, and joyful at the same time!
We won't cover Regular Expressions in this book because it's an endless topic. We will only note that Sublime Text uses the Boost's Perl Syntax for Regular Expressions; this can be found at http://www.boost.org/doc/libs/1_47_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
I recommend going to http://www.regular-expressions.info/quickstart.html if you are not familiar with Regular Expressions.
Let's open the Search panel by pressing Ctrl + F on Windows and Linux or command + F on OS X. The search panel options can be controlled using keyboard shortcuts:
Search panel options |
Windows/Linux |
OS X |
---|---|---|
Toggle Regular Expressions |
Alt + R |
command + Option + R |
Toggle Case Sensitivity |
Alt + C |
command + Option + C |
Toggle Exact Match |
Alt + W |
command + Option + W |
Find Next |
Enter |
Enter |
Find Previous |
Shift + Enter |
Shift + Enter |
Find All |
Alt + Enter |
Option + Enter |
As we can see in the following screenshot, we have the Regular Expression option turned on:
Let's try Search and Replace now by pressing Ctrl + H on Windows and Linux or Option + command + F on OS X and examining the following screenshot:
We can see that this time, both the Regular Expression option and the Case Sensitivity option are turned on. Because of the Case Sensitivity option being on, line 8 isn't selected, the pattern messages/(d)
doesn't match line 2 because d
only matches numbers, and the 1
on the Replace with field will replace match group number 1
, indicated by the parentheses around d
.
Let's see what happens after we press Ctrl + Alt + Enter for Replace All:
We can see that lines 2 and 8 still say messages
and not message
; that's exactly what we expected!
Incremental search is another cool feature that is here to save us keyboard clicks. We can bring up the incremental search panel by pressing Ctrl + I on Windows and Linux or command + I on OS X. The only difference between the incremental search and a regular search is the behavior of the Enter key; in incremental searches, the Enter key will select the next match and dismiss the search panel. This saves us from pressing Esc to dismiss the regular search panel.
Sublime Text also allows a multiple file search by pressing Ctrl + Shift + F or command + Shift + F
on OS X. The same shortcuts from the single file search also apply here; the difference is that we have the
Where field and a … button near it. The Where
field determines where the files can be searched for; we can define the scope of the search in several ways:
<open folders>
, <open files>
We can also combine filters by separating them with commas in the following manner:
/C/Users/Dan/Cool Project,*.rb,<open files>
This will look in all files in
C:UsersDanCool Project
that end with .rb
and are currently open by Sublime.
Results will be opened in a new tab called Find Results, containing all found results separated by file paths. Double clicking on a result will take you to the exact location of the result in the original file.