act on them collectively. After
you’ve begun using this feature, its power will become apparent to you, especially in editing code or any file
with a formal syntax.
Say, for example, that I’m
converting the following source code from Python 2 to 3. The first thing I want to do is rename “raw_input” to just “input”:
your_name = raw_input('Enter your name: ') print 'Hello,', your_name
printer_model = raw_input("What kind of printer do you have?: ") print your_name, 'has a', printer_model
Using Sublime Text, such a task is easy. I’ll select the first occurrence of “raw_input” and press Ctrl-d. Pay close attention, and you’ll notice that both occurrences of raw_input are now selected, each with its own blinking cursor. As I begin to type the word “input”, both occurrences are replaced. It is true that such a change could have been accomplished easily with search and replace, but I’ve only scratched the surface with multiple selection.
Next, I’ll want to replace the two
“print” statements with Python 3’s
“print” function, which means making the commands look like print(...).
SUBLIME TEXT LETS YOU SELECT MULTIPLE
NONCONTIGUOUS SPANS OF TEXT AND ACT
ON THEM COLLECTIVELY.
WWW.LINUXJOURNAL.COM / AUGUST 2013 / 79
times in this document, the last technique won’t work, so I’ll show you another way to make multiple selections. I’ll begin by positioning my cursor on the first print statement.
Then, I’ll hold Ctrl while clicking on the other print statement.
Although I haven’t selected any text, I have two blinking cursors.
Whatever I type will effect both lines.
I’ll type (, press end, and type ). Both lines received those keys, and now my file is Python 3-compliant.
There are several other ways of selecting multiple spans of text in
with them, you’ll get a feel for how to use them. Ultimately, when used productively, Sublime’s multiple
selection feature replaces most editor macro, find/replace operations and refactoring operations all at once.
Imagine, for example, how you could transform a plain-text list into an HTML <ul> list using multiple selection. If you can picture how this is done, you’re starting to grok multiple selection. If not, don’t fret:
this is a new editor feature. A few trips to YouTube and some video demos will help you get the idea.
Search and Replace
Forget grepping through your
codebase when the time comes for aggressive refactoring. Sublime Text offers a powerful recursive search and replace feature. Recursive search and replace eliminates the need for the GNU grep and find commands for many users. Many
editors provide recursive search and replace, although I find Sublime Text really gets it right in a way that few other projects do.
Click on Find→Find in Files, and a large search bar will appear at the bottom of your editor. Using the toggle buttons on the left, you can toggle regular expression matching,
Figure 4. Sublime Text can open up your search results in its own buffer. Click on any result to jump to its source.
WWW.LINUXJOURNAL.COM / AUGUST 2013 / 81
specify replacement text.
If you expect many results or plan to refer to your search results over time, toggle the “Use Buffer” icon in the search area. When enabled, Sublime Text will open a summary of search results in its own editor buffer.
When using a multihead workstation, I find it useful to put search results in one monitor and code in another.
Toggling “Show Context” will include a few lines before and after each hit in the results.
Sublime Text uses Perl-style regular expressions implemented using the Boost C++ library.
Sublime Text also supports regular expression replacements.
Snippets
Despite their best efforts to not repeat themselves, programmers often find themselves typing common blocks of text throughout their projects.
Examples include standard class file layouts, unit tests and license warnings programmers put at the top of each file.
To support this work flow, Sublime Text features “snippets”. Suppose I have a standard unit test layout for
"""
import unittest
class UnitTest(unittest.TestCase):
def setUp(self):
"""
Called before each test to set up the environment.
"""
pass
def tearDown(self):
"""
Called after each test to clean up.
"""
pass
def test<METHOD>(self):
pass
if _ _name_ _ == '_ _main_ _':
unittest.main()
I can use Tools→New Snippet.
Sublime Text will give me an
example snippet file. I’ll modify it to read like this:
<snippet>
<content><![CDATA[
"""
Unit tests for ${1:module} in ${1:project}.
"""
import unittest
class UnitTest(unittest.TestCase):
def setUp(self):
"""
Called before each test to set up the environment.
"""
pass
def tearDown(self):
"""
Called after each test to clean up.
"""
pass
def test${1:method}(self):
pass
if _ _name_ _ == '_ _main_ _':
unittest.main()
]]></content>
<tabTrigger>unittest</tabTrigger>
<scope>source.python</scope>
</snippet>
Notice that I’ve made a
“tabTrigger” tag and a “scope”
tag in my snippet file. Using the settings I’ve given, any time I’m in a Python file and type the word
“unittest”, I can press Tab, and the snippet will be inserted where the cursor is. To try it out, I’ll save the snippet as “unittest.sublime-snippet” in the default directory.
Now I can use the snippet to create unit tests quickly.
Packages Galore
Sublime Text can be scripted using plugins written in Python. These plugins are stored in packages that can be installed locally using a file manager or your favorite shell.
Should you feel the urge to scratch an itch no one else has found, you can write packages in Python (more on that later).
I like to think that Sublime
walks the fine line between an IDE and a text editor. Its speed and initial simplicity make it suitable for editing /etc files as easily as source code. Sublime’s true power, however, is found in add-on
packages users can write and install to do everything from synchronizing