Installing Sublime Text 2

Assuming you have access to the right repositories, you can also install Sublime via apt-get on Linux.

Preferences

Sublime configuration is done via JSON files. So the UI for configuring the text editor is simply a text editor. Same goes for project files, key bindings etc.

To modify the default preferences, go to the Preferences menu and select Settings-Default. Note that if you would rather like to make these settings user specific, select Settings - User as this applies there as well. The difference is that the default settings file already contains many settings that you might want to modify.

Here are some settings that you might want to change (look these variables up in the settings file and modify their value, you should not have to add them):

"rulers": [80],

"tab_size": 2,

"trim_trailing_white_space_on_save": true,

"ensure_newline_at_eof_on_save": true,

"translate_tabs_to_spaces" : true

The settings take effect as soon as you save the file.

If you've got a big monitor and are used to viewing more than one source file at a time, you can use the View->Layout feature to split the view up into columns and/or rows and look at multiple files at the same time. There's also the Shift+F11, distraction free view that allows you to see nothing but code! ?8-D Sublime also supports dragging tabs out into new windows as Chrome supports, so that might be useful as well.

One thing to be aware of when editing these JSON files is that Sublime's JSON parser is slightly stricter than what you might be used to from editing e.g. GYP files. In particular Sublime does not like it if you end a collection with a comma. This is legal: {"foo", "bar"} but not this: {"foo", "bar", }. You have been warned.

Project files

Here's a very simple project file that was created for WebRTC and should be saved in the parent folder of the trunk folder (name it webrtc.sublime-project). It's as bare bones as it gets, so when you open this project file, you'll probably see all sorts of files that you aren't interested in.

{

"folders":

[

{

"path": "trunk"

}

]

}

Here is a slightly more advanced example that has exclusions to reduce clutter. This one was made for Chrome on a Windows machine and has some Visual Studio specific excludes. Save this file in the same directory as your .gclient file and use the .sublime-project extension (e.g. chrome.sublime-project) and then open it up in Sublime.

{

"folders":

[

{

"path": "src",

"name": "src",

"file_exclude_patterns": [

"*.vcproj",

"*.vcxproj",

"*.sln",

"*.gitignore",

"*.gitmodules",

"*.vcxproj.*"

],

"folder_exclude_patterns": [

"build",

"out",

"third_party",

".git",

"Debug",

"Release"

]

}

]

}

Navigating the project

Here are some basic ways to get you started browsing the source code.

"Goto Anything" or Ctrl+P is how you can quickly open a file or go to a definition of a type such as a class. Just press Ctrl+P and start typing.

Open source/header file: If you're in a header file, press Alt+O to open up the corresponding source file and vice versa. For more similar features check out the Goto->Switch File submenu.

"Go to definition": Right click a symbol and select "Navigate to Definition". A more powerful way to navigate symbols is by using the Ctags extension and use the Ctrl+T,Ctrl+T shortcut. See the section about source code indexing below.

Enable source code indexing

For a fast way to look up symbols, we recommend installing the CTags plugin. we also recommend installing Sublime's Package Control package manager, so let's start with that.

Once installed, you'll get an entry in the context menu when you right click the top level folder(s) in your project that allow you to build the Ctags database. If you're working in a Chrome project however, do not do that at this point, since it will index much more than you actually want. Instead, do one of:

Create a batch file (e.g. ctags_builder.bat) that you can run either manually or automatically after you do a gclient sync:

This takes a couple of minutes to run, but you can work while it is indexing.

Edit the CTags.sublime-settings file for the ctags plugin so that it runs ctags with the above parameters. Note: the above is a batch file - don't simply copy all of it verbatim and paste it into the CTags settings file :-)

Building with ninja

Assuming that you've got ninja properly configured and that you already have a project file as described above, here's how to build Chrome using ninja from within Sublime. For any other target, just replace the target name.

Go to Tools->Build System->New build system and save this as a new build system:

{

"cmd": ["ninja", "-C", "out\\Debug", "chrome.exe"],

"working_dir": "${project_path}\\src",

"file_regex": "^(.*)\\(([0-9]*)\\)(\\s:\\s)(.*)"

}

On Linux and Mac, fix the targets up appropriately, fwd slash instead of backslash, no .exe, etc

This will make hitting Ctrl-B build chrome.exe (really quickly, thanks to ninja), F4 will navigate to the next build error, etc. If you're using Goma, you can play with something like: "cmd": ["ninja", "-j", "200", "-C", "out\\Debug", "chrome.exe"],.

You can also add build variants so that you can also have quick access to building other targets like unit_tests or browser_tests. You build description file could look like this:

Save this file as run_lint.py (Sublime will suggest the right location when you save the plugin - Packages\User).

You can run the command via the console like so:

view.run_command('run_lint')

Note that here's an interesting thing in how Sublime works. CamelCaps are converted to lower_case_with_undescore format. Note also that although the documentation currently has information about "runCommand" member method for the view object, this too is now subject to that convention.

Taking this a step further, you can create a keybinding for your new plugin. Here's an example for how you could add a binding to your User key bindings (Preferences->Key Bindings - User):

[

{

"keys": ["ctrl+shift+l"], "command": "run_lint"

}

]

Now, when you hit Ctrl+Shift+L, cpplint will be run for the currently active view. Here's an example output from the console window:

Compile current file using Ninja

As a more complex plug in example, look at the attached python file: compile_current_file.py. This plugin will compile the current file with Ninja, so will start by making sure that all this file's project depends on has been built before, and then build only that file.

First, it confirms that the file is indeed part of the current project (by making sure it's under the <project_root> folder, which is taken from the self.view.window().folders() array, the first one seems to always be the project folder when one is loaded). Then it looks for the file in all the .ninja build files under the <project_root>\out\<target_build>, where <target_build> must be specified as an argument to the compile_current_file command. Using the proper target for this file compilation, it starts Ninja from a background thread and send the results to the output.exec panel (the same one used by the build system of Sublime Text 2). So you can use key bindings like these two, to build the current file in either Debug or Release mode:

If you are having trouble with this plugin, you can set the python logging level to DEBUG in the console and see some debug output.

Format selection (or area around cursor) using clang-format

Copy buildtools/clang_format/scripts/clang-format-sublime.py to ~/.config/sublime-text-3/Packages/User/ (or -2 if still on ST2) and add something like this to Preferences->Key Bindings - User:

"keys": ["ctrl+shift+c"], "command": "clang_format",

Miscellaneous tips

To synchronize the project sidebar with the currently open file, right click in the text editor and select "Reveal in Side Bar". Alternatively you can install the SyncedSideBar sublime package (via the Package Manager) to have this happen automatically like in Eclipse.

If you're used to hitting a key combination to trigger a build (e.g. Ctrl+Shift+B in Visual Studio) and would like to continue to do so, add this to your Preferences->Key Bindings - User file:

If you want to take that a step further, add an entry to the right-click context menu by creating a text file named "context.sublime-menu" under "%APPDATA%\Sublime Text 2\Packages\User" with the following content:[ { "command": "open_include", "caption": "Open Include" } ]