Boost productivity in Sublime Text

James Brooks explores ways to improve your productivity in Sublime Text, using plugins, projects and custom key bindings.

Sublime Text is a sophisticated text editor for writing code or markup. In this article, I'll look at three ways to push it harder and boost your productivity: setting up and working with projects, extending Sublime

Text's core functionality with plugins, and using custom key bindings. Everything should be broadly compatible with Sublime 2 and 3.

Projects

Development projects can easily run to hundreds of lines of code in hundreds of files. Sublime Text includes an easy-to-use project-switching system, providing us with a simple way of managing projects. Sublime will create two files for a project: Project. sublime-project and Project.sublime-workspace.

The former contains the name of the project and the location of any directories within it, and can be used to override Sublime settings. Let's take a look at the methods Sublime has to create projects:

  • Inside a blank Sublime window drop a directory (or multiple folders) onto the editor space
  • Click Project > Add Folder to Project and select your directory. Sublime Text will open it
  • Click File > Open and select your directory. This is the same as opening a file
  • Use the command line tool subl by running subl. or by specifying the path to your directory. The arguments we send to subl tell Sublime to open the folder from the current directory

Once your directory is open, save your project somewhere on your system – or if you want to share your Sublime Text configuration on a per-project basis, save the project in your project folder. If you want to edit the title, go to Project > Edit Project and add "name": "Project Name" above the path line.

"But James, I have multiple projects and I need to switch between them," I hear you cry. No problem. Select Project > Quick Switch Project... . Type the project name, hit Enter and Sublime will open the project.

Alternatively, use a keyboard shortcut: Ctrl+Alt+P for Windows or Linux, and Cmd+Ctrl+P for OS X.

Now let's look at how to configure projects. You can edit project preferences in the same way you changed the project name earlier. I'll use the example of displaying rulers at different widths. Underneath the folders key, we need to add a new settings object.

{
    "folders":
    [
        {
            "name": "My Awesome Project",
            "follow_symlinks": true,
            "path": "/Users/jamesbrooks/code/project"
        }
    ],
    "settings": {
        "rulers": [60, 80, 160]
    }
}

Any setting that you can add to your user preferences can be added in the settings object. If you switch to a different project or start a new one, Sublime will revert to your user preferences, or its defaults.

Finally, let's look at adding a custom build system to a project. In some cases, your project may be using a customised build system which you may not need when using other projects. In Sublime, build systems can also be used to run shell commands.

For instance, you can count the lines of code in a project, send an email, display notifications, or automatically push to production after running your test suite. Underneath the settings object we just added, we'll need to add a build_systems array:

{
    "folders":
    [
        {
            "name": "My Awesome Project",
            "follow_symlinks": true,
            "path": "/Users/jamesbrooks/code/project"
        }
    ],
    "settings": {
        "rulers": [60, 80, 160]
    },
    "build_systems": [
        {
            "name": "Lines of code",
            "working_dir": "${folder}",
            "shell_cmd": "sloc ."
        }
    ]
}

In this case, Sublime will output a breakdown of the project code, using sloc.

Plugins

While Sublime comes with a great default set-up, it's also possible to install third-party plugins – and with Will Bond's Package Control (sublime.wbond.net), installing a plugin is as easy as knowing its name. Let's look at some of the best plugins around:

  • SublimeLinter - This is a framework for code linting – automatically reading through your source code looking for sections that may not work as intended. This will reduce the time you spend compiling, refreshing and running, as you'll see errors inline.
  • SublimeCodeIntel - SublimeCodeIntel is a port of a plugin from the Open Komodo editor (openkomodo.com), providing support for the majority of popular languages. Similar to SublimeLinter, this reads your code and allows you to jump to declarations for variables and methods.
  • SublimeGit (with GitGutter) - This provides you with a load of Command Palette integration, integration with Legit (git-legit.org) and gitflow (github.com/nvie/gitflow), and the ability to set custom Git commands. Installing GitGutter on top of SublimeGit displays per-line Git differences inline, making it easy to see your changes.

Key Bindings

Sublime comes with loads of cool default key bindings which make interacting with it much easier. Key bindings can also be configured for particular operating systems, so you can set up key bindings to use Cmd in place of Ctrl on OS X, for example.

When adding custom key bindings, you should always add them to the user file. Click Preferences in the menu bar (part of the Sublime Text menu on OS X) then select Key Bindings – User .

If you're unsure of the existing commands, open Key Bindings – Default . Let's look at how to create a custom key binding for Edit Project . On OS X:

{ "keys": ["super+ctrl+e"], "command": "open_file", "args": {"file": "${project}"} }

On Windows and Linux:

{ "keys": ["ctrl+alt+e"], "command": "open_file", "args": {"file": "${project}"} }

The same open_file command can be called with different file parameters to make editing other files even faster too.

Ever find yourself with a file open but absolutely no idea where it lives? You need the Reveal in Sidebar command. Installing the SideBarEnhancements plugin with this key binding will make you a sidebar master in no time! In OS X:

{ "keys": ["super+ctrl+r"], "command": "refresh_folder_list" }

That concludes our look at how to boost your productivity enhancements in Sublime Text. Now you're on the path to Sublime mastery.

Words: James Brooks

James Brooks is an expert in Sublime Text, PHP and Node.js. Follow him on Twitter at @jbrooksuk. This article originally appeared in net magazine issue 259.

Like this? Read this!