Developerator

Sublime Text Wizardry

I recently did a demo on tricks you can do with Sublime Text every developers needs to know at the Melbourne Developer Meetup, October 2014.

Here you will find a list of the items I went through and how you can replicate them for yourself. I recommend Sublime Text 2 as version 3 is still in beta and many plugins out there don't support Sublime Text 3 just yet.

Go to

Mac: Command + P
Windows/Linux: Control + P

It opens an omnibar where you can type in what you're looking for and it finds it for you.

Go to file

Type the file name you're after, if the file you're looking for is inside other directories, you can punch in the directory names or parts of it as well, e.g. file is /contrib/somelibrary/somefile.cpp then you can type in consolif.c co: contrib soli: somelib f.c: somefile.cpp. It should get you to the file(s) the pattern matches to.

Go to line

Add a colon : to the end of what you've typed before, e.g. cosolif.c:100 will take you to line 100 of the file. If you just enter :100 it will take you to the line 100 of the currently open file.

Go to definition

Assume somefile.cpp has some function definitions in it. One of which is called somefunction(). It doesn't need to be a C++ file, it can be pretty much any language that your Sublime Text installation supports. In the omnibar type in [email protected], it should take you to the very line somefunction() has been defined. Not specifying the filename will take you to the function definition of the currently open file. The definition can also be a class or module name.
For occurrence of words that or not necessarily definitions, use # instead of @.

Bookmarks

Mac: Command + F2 (toggle bookmark), F2 (go to bookmark)
Windows/Linux: Control + F2 (toggle bookmark), F2 (go to bookmark)

You can easily create bookmarks in files and switch back and forth between them. A line that has at least one bookmark in it will show a right arrow on the left-hand side. Note that bookmarks are not applied to lined but a specific column in a row. Therefore you can have multiple bookmarks in one line. This can cause a bit of confusion as being on a line that has bookmark is not going to be enough in order to remove a bookmark. You have to be on the exact location the bookmark was originally created at.

Commands Palette

Mac: Command + Shift + P
Windows/Linux: Control + Shift + P

Opens an omnibar that allows you to type in commands, e.g. Bookmarks: Select All, Git: Diff Current File (if you have the Git plug-in installed), snippets, etc.

Set Syntax

Usually Sublime Text recognises the type of the file you're working on and does proper syntax highlighting and settings for that Syntax. In case it doesn't, for example when you create a new file, in the Command Palette, type in synltx which stands for Syntax: LaTeX as an example.

Snippets

You would usually type in the Snippet you're after and press Tab to complete it, e.g. in a PHP file, you'd type for and press Tab. But that required you to know the Snippet in advance. To quickly see a list of all available snippets in the current Syntax, open up the Command Palette and type in snippet:. You can see the shortcut associated with that Snippet, as well as pressing Enter to use it. Try lorem.

Newline

Mac: Enter
Windows/Linux: Enter

Natural

Assume you're working on a line and the caret is in the middle of the line, in front of the letter i of the word arbitrary. When you press Enter, it breaks the line and the word in two, making it look like

.... .. .... .. .... .. .... ... .. arbit
rary ... .. . .. ..... ..

Underneath

Mac: Command + Enter
Windows/Linux: Control + Enter

Will put the caret on the next line without breaking the word nor the current line.

Above

Mac: Command + Shift + Enter
Windows/Linux: Control + Shift + Enter

Will put the caret on the previous line without breaking the word nor the current line.

Preferences

Default & User

Preferences in Sublime Text are text-based (JSON Objects to be exact), and are either scoped Default or User. The Default Preferences belong to the editor and its packages. Although the Default settings can be edited, they can potentially be overwritten by an update later on. Therefore you should always put your settings inside the User settings file.

Settings

Sublime Text settings can be accessed form the top menu. Click on Sublime Text > Preferences > Settings - Default or Settings - User. You should copy the contents of the Default settings file into the User settings file and make your changes there. I have shared my own settings file in this repository on GitHub.

Key Bindings

Same as above goes for Key Bindings. This is where shortcuts are defined. If you want to change a Sublime Text built-in shortcut, then you should override the entry in Key Bindings - User file. For example the following will change the Next Bookmark key from F2 to F7.

[
    { "keys": ["f7"], "command": "next_bookmark" }
]

You can override package specific shortcuts in Key Bindings - User as well.

Project Settings

Settings can be project specific. You might want to have a 2-space tab size on a Drupal project you're working on and 4-spaces on any other project. When you save your project in Sublime Text, from the top menu Project > Save Project As... open your projects .sublime-project file and add the following to the Project JSON object.

{
    "folders": [
        {
            "path": "/path/to/drupal/project"
        }
    ],
    "settings": {
        "tab_size": 2
    }
}

More info here.

Syntax Specific Settings

Sometimes settings need to be Syntax specific. For example Ruby coding style prefers a 2-space tab size. Or in Markdown, you might want to allow trailing spaces whereas on any other file, you don't. Having the following in /path/to/Sublime Text 2/Packages/User/Markdown.sublime-settings will just do so.

{
    "trim_trailing_white_space_on_save": false
}

Folding

Sublime Text is pretty smart in recognising blocks of code or text. For example Sublime Text will create two blocks for the following text. Please note the indentation.

item #1
    subitem #1.1
    subitem #1.2
    subitem #1.3
item #2
    subitem #2.1
    subitem #2.2
item #3
    subitem #3.1
item #4

Note how the items #3 and #4 will not turn into foldable blocks. Command + Option + [ fold the current block your caret is on, Command + Option + ] unfolds it. Use Control + Shift + [ and Control + Shift + [ on Windows/Linux.
Command + K, Command + 1 through Command + K, Command + 9 sequences fold blocks from level 1 through level 9. For example Command + K, Command + 3 will fold blocks that you have in a block within another block. Command + K, Command + 0 unfolds all. Use Control instead of Command on Windows/Linux.

Moving Lines

You can move one line or multiple selected lines up and down. Command + Control + and Command + Control + move the selected line(s) up and down respectively. Use Control + Shift + and Control + Shift + on Windows/Linux.

Multi-cursor

Mac: Command + Click
Windows/Linux: Control + Click

One of the nicest features of Sublime Text is that you can have multiple carets which allows simultaneous editing. There are different ways to create multiple cursors. Easiest one is by holding Command on Mac, Control on Windows/Linux and clicking on different areas in your document.
One of the useful things you can do with multiple cursors is that when you copy contents of multiple cursors into clipboard, you can paste them onto multiple cursors as well. As long as the number of cursors match everything should be pasted in the corresponding cursor location.

Select Next Like

Select a word, then press Command + D on Mac or Control + D on Windows/Linux to select the next occurrence of the selected word.

Select All Like

Select a word, then press Command + Control + G on Mac, Alt + F3 on Windows/Linux to select all occurrences of the selected word.

Select Lines

Select two or more lines, press Command + Shift + L on Mac, Control + Shift + L on Windows/Linux to turn the selection into a multi-line multi-cursor selection.

Visual Block

Hold down Option then click on the location you want to start a visual block selection and drag. This will select your content in a box, rather than the traditional way which wraps the lines as well. Use WinKey + Shift + Click on Windows/Linux.

Regex in Find and Find/Replace

Mac: Command + F (find in document), Command + Shift + F (find in project)
Windows/Linux: Control + F (find in document), Control + Shift + F (find in project)

Sublime Text allows you to search in Regular Expressions if you are looking for patterns that cannot be found using normal search. For example, in a document you wish to find all email addresses and make them all upper case. To do so, you open Find, press Command + Option + R to enable Regex search (You can also click on the button on the left-hand side of the search box). Then type in \S+@\S+ (simple email regex for the sake of the argument). Now click on Find All or press Option + Enter on Mac, Alt + Enter on Windows/Linux. Now you have all email addresses in the document selected, press Command + K, Command + U sequence to make them all upper case.

Packages

Below is a list of packages I use on a daily basis.

Package Control

The package every single Sublime Text user needs to have is a package management package called Package Control. It's very easy to install and makes installing and uninstalling packages in your installation extremely simple.
Note that the code used for installing Package Control for Sublime Test 2 and 3 are different. Once installed, you can open the Command Palette and type in Package Control: Install Package, then enter the name of the package you would like to install.
If there's a Syntax that Sublime Text doesn't support out of the box, then you can install the appropriate package, e.g. Puppet.

Markdown Preview

Makes previews of your markdown documents. Open up the Command Palette and pick Markdown Preview: Preview in Browser.

SidebarEnhancements

Adds useful items to the left-hand sidebar.

Alignment

Aligns your definitions. You won't need to worry about spacing them manually anymore. All you need to do is to select the lines you want to align and press Command + Control + A on Mac, and Control + Alt + A on Windows/Linux. It turns

enum survey_info_flags {
    SURVEY_INFO_NOISE_DBM = 1<<0,
    SURVEY_INFO_IN_USE = 1<<1,
    SURVEY_INFO_CHANNEL_TIME = 1<<2,
    SURVEY_INFO_CHANNEL_TIME_BUSY = 1<<3,
    SURVEY_INFO_CHANNEL_TIME_EXT_BUSY = 1<<4,
    SURVEY_INFO_CHANNEL_TIME_RX = 1<<5,
    SURVEY_INFO_CHANNEL_TIME_TX = 1<<6,
};

into

enum survey_info_flags {
    SURVEY_INFO_NOISE_DBM             = 1<<0,
    SURVEY_INFO_IN_USE                = 1<<1,
    SURVEY_INFO_CHANNEL_TIME          = 1<<2,
    SURVEY_INFO_CHANNEL_TIME_BUSY     = 1<<3,
    SURVEY_INFO_CHANNEL_TIME_EXT_BUSY = 1<<4,
    SURVEY_INFO_CHANNEL_TIME_RX       = 1<<5,
    SURVEY_INFO_CHANNEL_TIME_TX       = 1<<6,
};

Git

Provides useful Git command. Diff, Checkout Current File, Log, Change Branch, Blame are the ones I use the most.

GitGutter

Shows what has changed in the file you have open. It's very useful to see what lines have changed, what lines are new, and where lines are missing.

Vintage

Vintage is a package that comes with Sublime Text but is disabled by default. If you like Vi or Vim, I highly recommend this package. You can enable it by either manually modifying ignored_packages in the settings file, or using Package Control's Enable Package command.

Emmet

I don't personally use this but web developers would appreciate Emmet. It receives CSS selectors and turns them into HTML, e.g. div#album>ul>li*5>img[alt="Image #$"] will turn into

<div id="album">
    <ul>
        <li><img src="" alt="Image #1"></li>
        <li><img src="" alt="Image #2"></li>
        <li><img src="" alt="Image #3"></li>
        <li><img src="" alt="Image #4"></li>
        <li><img src="" alt="Image #5"></li>
    </ul>
</div>

More info on emmet.io.

Themes

There are ton of Themes available for Sublime Text. ColorSublime is a nice gallery that has a very decent size collection and live previews and a guide on how to install a theme.

For additional resources, please check Sublime Text's both official and unofficial documentations.