





















































(For more resources related to this topic, see here.)
Search and Replace is one of the common actions we use in every 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 article 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.
We can also refer to the group by using $1 instead of 1.
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:
We can also combine all the filters by separating them with commas. We can do it in the following manner:
/C/Users/Dan/Cool Project,*.rb,<open files>
This will look in all files in C:UsersDanCool Project that ends 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 get you to the exact location of the result in the original file.
Multiple Selections is one of Sublime's coolest features; TextMate users might be familiar with it. So how can we select multiple lines? We select one line like we usually do and selecting the second line while holding Ctrl or command on OS X. We can also subtract a line by holding the Alt key or command + Shift keys on OS X. This feature is really useful so it is recommended to play with it, the following are some shortcuts that can help us feel more comfortable with multiple selections:
Multiple Selection action |
Windows/Linux |
OS X |
Return to Single Selection Mode |
Esc |
Esc |
Undo last selection motion |
Ctrl + U |
command + U |
Add next occurrence of selected text to selection |
Ctrl + D |
command + D |
Add all occurrences of selected text to selection |
Alt + F3 |
Control + command + G |
Turn Single Linear Selection into Block Selection |
Ctrl + Shift + L |
Shift + command + L |
The Column Selection feature is one of my favorites! We can select multiple lines by pressing Shift and dragging the right mouse button on Windows or Linux and pressing Option and dragging the left mouse button on OS X. Here we want to remove the letter s from messages, as shown in the following screenshot:
We have selected all s using Column selection; now we just need to hit backspace to delete them.
Sublime is known for its ability to quickly move between and around files and lines. Here, we are going to master how to navigate our code quickly and easily.
We already learned how to use the Go To Anything feature, but it can do more than just searching for filenames. We can conduct a fuzzy search inside a "fuzzily found" file. Really? Yeah, we can. For example, we can type the following inside the Go To Anything window:
isl#wld
This will make Sublime perform a fuzzy search for wld inside the file that we found by fuzzy searching isl; it can thus find the word world inside a file named island.
We can also perform a fuzzy search in the current file by pressing Ctrl + ; in Windows or Linux and command + P, # in OS X. It is very common to use fuzzy search inside HTML files because it will immediately show all the elements and classes in order to accelerate navigation.
Sometimes we want to search for a specific function or specific class inside the current file. With Sublime we can do it simply by pressing Ctrl + R on Windows or Linux and command + R on OS X.
Project is a group of files and folders. To save a project we just need to add folders and files to the sidebar, and then from the menu, we navigate to Project | Save Project As…
The saved file is our projects data, and it is stored in a JSON formatted file with a .sublime-project extension. The following is a sample project file:
{ "folders": [ { "path":"src", "follow_symlinks":true }, { "path":"docs", "name":"Documentation", "file_exclude_patterns":["*.xml"] } ], "settings": { "tab_size":6 }, "build_systems": [ { "name":"List", "shell_cmd":"ls -l" } ] }
As we can see in the preceding code, there are three elements written as JSON arrays.
Each folder must have a valid folder path that can be absolute or relative to the project directory, which is where the project file is. A folder can also include the following keys:
The project-specific settings array will contain all the settings that we want to apply only on this project. These settings will override our user settings.
In an array of build system definitions, we must specify a name for each definition; these build systems will then be specified in Tools | Build Systems.
For more information about build systems, please visit http://sublimetext.info/docs/en/reference/build_systems.html.
To switch between projects quickly, we can press Ctrl + Alt + P in Windows or Linux and Control + command + P in OS X.
By now, we have learned some of Sublime's basic features to the most advanced features and techniques that need to be used while editing code.
Further resources on this subject: