Sublime Text 2 Project Bliss

Tutorial Details
  • Difficulty: Intermediate
  • Completion Time: 30 Minutes

One of the many things that led me to decide that Sublime Text was going to be my text editor of choice was the way you could customize what’s shown in the file list on the left. I had no idea that this level of customization was available. Here’s a short tutorial that I hope will help you as much as it did me.


Step 1 – Adding a Folder

Note: If you already have a project set up, skip to Step 3.

Upon opening up Sublime Text, add a folder to the project by pressing Ctrl+Shift+P (Cmd+Shift+p on Mac) to bring up the über handy Command Palette, typing “Add”, and then hitting Enter.


Step 2 – Save the Project

Now that you’ve added a folder to the project, bring up the command palette again, and type “Save” (or Project: Save As) to save your project. We’re going to be editing this file in the next step.

Don’t forget to give your project file the .sublime-project extension. Likely, this will be provided for you.


Step 3 – Get to Know the Project File

From the menu, select Project -> Edit Project to edit the project file you saved in Step 2. Or, of course, access this from the command palette.

You should now see something similar to the following in your editor window:

{
	"folders":
	[
		{
			"path": "/C/wamp/www/wordpress"
		}
	]
}

Fairly self explanatory, right? You’ll notice that, as with all Sublime Text goodies, this file is formatted as JSON. Don’t stress too much, if you don’t know much about JSON. You should be able to figure it out as you go along.

3.1 – Displaying a Particular Folder

Let’s now update it so that it points to our theme folder/directory:

{
	"folders":
	[
		{
			"path": "/C/wamp/www/wordpress/wp-content/themes/twentyeleven"
		}
	]
}

Now, keep an eye on the sidebar on the left and hit Ctrl+S (Cmd+S on a Mac) to save the file. The sidebar will be updated instantly with the new folder.

The next thing worth pointing out (if you haven’t already noticed) is that the folders object is made up of an array literal (notated by the square brackets). If you’ve figured out that this means that we can specify more than one folder for our project, then you’d be absolutely correct. Let’s go ahead and give it a go.

3.2 – Adding Multiple Folders

Fear not plugin developers, we care about you too, so let’s add our plugins directory as well as the theme folder:

{
	"folders":
	[
		{
			"path": "/C/wamp/www/wordpress/wp-content/themes/twentyeleven"
		},
		{
			"path": "/C/wamp/www/wordpress/wp-content/plugins"
		}
	]
}

Save the file again, and you should see the following:


Step 4 – What Else Can We Do?

Sublime Text 2 is an amazing tool in your web development arsenal

I’m glad you asked! For starters, within each object in the folders array, there are a few different options available to us:

  1. path (string): As we’ve seen, lets you specify the folder/directory to show in the sidebar
  2. (string): Specify a name for the item in the sidebar
  3. file_exclude_patterns (array): Specify files to exclude
  4. folder_exclude_patterns (array): Specify folders to exclude

The path is already familiar to us, and the name is self explanatory, so let’s dig in to the last two.

4.1 – Excluding Files

Admit it: there are some files that you will simply never edit, and these files are probably cluttering your project view. Instead, why don’t we hide them, and then, on the very odd occasion that you actually do need to edit them, open them manually.

This bit comes down to preference, but here are the files that I typically exclude: '._', .bak, .ico, .sample, .swf, .tar, .tgz, files beginning with 'wp-' (let’s not go editing the WordPress core files), and finally, .zip files. To do this, our file_exclude_patterns array should look as follows:

"file_exclude_patterns":[
	"._*",
	"*.bak",
	"*.ico",
	"*.sample",
	"*.swf",
	"*.tar",
	"*.tgz",
	"wp-*.php",
	"*.zip"
]

4.2 – Excluding Folders

If you don’t want to specify each file individually, you can instead choose to exclude folders. This is the list that I generally use:

"folder_exclude_patterns": [
	"cgi-bin",
	"wp-admin",
	"wp-includes"
]

Note: You need to specify the file_exclude_patterns and the folder_exclude_patterns for each folder (see the final example below).


Step 5 – But Wait… There’s More!

Other than the folders option, you can also specify two others:

  1. settings: Any of the usual list of settings that you would like to apply on a project specific basis can go in here (for example: tab_size).
  2. build_systems: This allows you to add project-specific build system (which is beyond the scope of this tutorial).

Our Final Project File

Here is an example of what your final .sublime-project file might look like (shortened slightly to be more succinct):

{
	"folders":
	[
		{ // theme
			"path": "/C/wamp/www/wordpress/wp-content/themes/twentyeleven",
			"name": "Twenty Eleven Theme",
			"file_exclude_patterns":[
				"._*",
				"*.ico",
				"*.swf"
			],
			"folder_exclude_patterns": [
				"images"
			]
		},
		{ // plugins folder
			"path": "/C/wamp/www/wordpress/wp-content/plugins",
			"name": "Plugins Folder",
			"file_exclude_patterns":[
				"._*", // you need to specify this *again*
				"*.bak",
				"*.sample",
				"*.tar",
				"*.tgz",
				"*.zip"
			],
			"folder_exclude_patterns": [
				"akismet"//,
				// add any other plugins you wish to exclude
			]
		}
	],
	"settings":
	{
		"tab_size": 4
	}
}

To Conclude

Note: If you’re itching to further harness your Sublime Text 2, stay tuned for “Perfect Workflow in Sublime Text 2″ – a free-to-all course from Tuts+ Premium, coming late September.

Sublime Text 2 is an amazing tool in your web development arsenal, and it’s features and possibilities reach way beyond this tutorial. For more on what it can do, be sure to dig around in the documentation… you’re guaranteed to find some real gems hiding away in there.

Links:

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.stepquick.net Step

    Great quick tips for Sublime Text 2. I’ve been really enjoying it and it’s nice to see there’s a way to customize the projects that much.

    So it is possible to have it exclude images as well? Judging by the list of excludes likely.

    • http://pochen.me/ Po Chen

      Yes, anything you wish

  • http://varemenos.com Varemenos

    Wow, didn’t expect this to be possible. Thanks for sharing!

  • http://en.webdesignhome.ro Radu

    Indeed, the large customization options available in Sublime Text 2 made me change my old editor. ;)

  • http://www.austinkpickett.com Austin Pickett

    Had recently picked up ST2 and SFTP with it– they’re both amazing utilities to have as a web developer.

    Great article.

  • Jesus Bejarano

    Good stuffs. Never underestimate the power of “Ctrl + shift + p”.

  • toufik

    I had no idea that this level of customization was available.

    talk about customization, maybe you should try Vim.
    i am no against sublime text 2, is second beast text editor in my opinion :)

  • http://remy.bach.me.uk Remy

    @toufik: I’m very familiar with Vim actually. It’s really powerful!

    Have you tried using Vintage mode in Sublime? If not, you should definitely look it up (http://www.sublimetext.com/docs/2/vintage.html), and then install the VintageEx package (https://github.com/SublimeText/VintageEx).

    • toufik

      yes, vintage mode are cool feature, especially for my finger ;-) but i still feel missing something.
      anyway, thanks for vintageEx, i’ll definitely give a try.

      • Jesus Bejarano

        You know that you can change the keybidings in the vintage_mode package , and configure the whole key mapping to your like? { “keys”: ["ñ","ñ"], “command”: “exit_insert_mode”,
        “context”:
        [
        { "key": "setting.command_mode", "operand": false },
        { "key": "setting.is_widget", "operand": false }
        ]
        },

        { “keys”: ["j","j"], “command”: “exit_visual_mode”,
        “context”:
        [
        { "key": "setting.command_mode"},
        { "key": "num_selections", "operand": 1},
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": false }
        ]
        },

        { “keys”: ["j","j"], “command”: “vi_cancel_current_action”, “context”:
        [
        { "key": "setting.command_mode" },
        { "key": "vi_has_input_state" },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": false }
        ]
        }

      • Alejandro

        Tried to implement the snippet into KeyBindings :: User
        Unfortunately, the following error did not allow me to save the config.

        Error trying to parse file: Unexpected trailing characters in ~/Library/Application Support/Sublime Text 2/Packages/User/Default (OSX).sublime-keymap:6:2

        Any help would be appreciated.

  • http://www.guyroutledge.co.uk Guy Routledge

    Great article, I had no idea you could customise the sidebar like that! Really useful tip.

    For an extra bit of Sublime sidebar goodness, I use a plugin called Sidebar Enhancements (https://github.com/titoBouzout/SideBarEnhancements) which brings loads more features to the right-click menu in the sidebar.

  • http://www.timwestdesigns.com Tim

    Thanks for the great info. I never really bother with the project feature. I tend to just open up the root folder using ST2 and browse the sidebar to open files. Having seen how useful and easy it is to use though, I will definitely be trying this out soon.

  • adfsaf23423

    Here’s my ST2 project quickstart. The tactic in the article seems interesting but superfluous. http://www.screenr.com/Dyo8

    • http://jarrydcrawford.com.au/ Jarryd

      Wow, that owns! Yoink! :D

  • David Basilio

    Baa valeu !

  • http://justinwhall.com Justin W Hall

    I love ST2. As a mac user, my only real beef is the fact I can’t drag and drop files in our of the file navigator.

    • http://remy.bach.me.uk Remy

      Are you sure you can’t do that? I’ve just tried (in OSX 10.7.4) and it works for me.

      • http:28inch.co.uk 28inch

        There is a far more easier way to do projects, using Bit101`s “STProjectMaker” plugin.
        It even has a ‘Nettuts Fetch’ like option for pulling assets dynamically.

        http://www.bit-101.com/blog/?p=3512

    • http://blog.marcomonteiro.net Marco Monteiro

      Well my guess is that you can’t do that because it’s a bad practice to do so.

      • http://www.jvsoftware.com Javier Villanueva

        How so?

  • Brandon

    I wish instead of hiding files you could set them to not open on click. Like images, I want to know they’re there, I just don’t want to see the guts of the file when i’m trying to right click to move/upload/delete.

    Same thing with excluding folders. I don’t need to see the wp-includes folder when i’m trying to open a file, but I like being able to see it if i need to reference a function.

  • Andreas

    I’ve been trying to get used to ST2, but I miss some features that my old editor have (PHPEd). The first feature is that I can ctrl-click on php function to jump to the file and function directly. How can I do this in ST2?

    The second thing is when I by mistake type some php syntax error, like a missing ; or something like that. In PHPEd the line where the error is get marked so I can easily find where the error is. How can I do this in ST2?

    Thanks for the great ST2 tips! More tips is welcome!
    /Andreas

    • makebei

      Not quite the same as CTRL+Clicking, but you can hit CTRL+R (CMD+R on Mac I think) and it will display a list of all the functions in the current file. I believe it is limited to the current file.

      There’s also this Goto-Symbol plugin available via Package Control which may do something similar to what you’re after.

      For syntax checking, a quick search in Package Control for PHP brought up this PHP Syntax Checker. I haven’t tried any though, so not sure if it’s any good.

  • Clark Pan

    Heres a small tip, the “path” variable can be a relative path.

    I find it more useful to set the path up as “./” and put the project file in my project folder so when i’m working on other systems (or the project file was checked into a repo), the correct folders will be setup.

    • Jon

      Thanks. Just pulled a project file from a repo run on OSX onto Windows and it didn’t work obviously. This tip fixed it!

    • http://twitter.com/artoric Luka Kemperle

      Dear Clark,
      Thanks for your comment! It works like a charm on two computers!

    • torrance scott

      This tip was great. I set up all my projects in Dropbox (or cloud sync of choice), and now can open on any computer regardless of different hard drive letters etc. I don’t know why ST2 doesn’t default with everything as a relative path. I also save all my project files in a single folder so they don’t clutter my projects.

  • J Cole Morrison

    Wow, that’s some great advice for sure. Thanks so much!

  • EyeDentify

    I have this small but irritating beef with ST2 that on Win7 when i open a folder it opens a new ST2 window with the folder instead of just open the folder in the current window sidebar…

    How could i get this to work the way i intend to ?

  • http://www.witnesstochange.co.uk Shaun

    Another one to add is the SFTP plug-in which goes very nicely with the Project stuff, use it all the time.

  • chichibek

    as a ubuntu user i was tired to deag and drop proyects directories from path to sublime

    thanks for the advices

  • http://brandonbrown.io Brandon

    Is there any way to sync .sublime-sorkspace files between OS X and Windows? I have a Work computer (Windows) and a personal computer (OS X) and use ST2 on both. I Use Dropbox to sync packages but have no for sure way of syncing my workspace files, unless it just works (have yet to test on my MacBook as I’m at work at the moment)

  • Jonatas C D

    nice

    I posted in my blog how to use virtualenv, django with Sublime Text 2:

    Sublime Text, virtualenv, TDD e Django

    it is in portuguese, but the code is understandable

    cheers!

  • Preston

    I was hoping for a bit more with this article, but it’s a nice refresher on building out custom project files.

    I’ve been using ST2 fulltime for a few months now, and the only real thing I wish they would add is the ability to dock/undock certain panels. I work with 4 monitors, with my text editor resting in a vertically aligned screen.

    I have become accustomed to hitting CTRL+K CTRL+B to hide the file list, but I would absolutely love it if I could just tear the file list off that window and place in on another screen.

  • mdennisa

    I worship the creator of ST2, including the one who post awesome article here!

  • Tobias

    Nice tips. However: project wise: why add all the files to begin with? I usually just add the theme folder and wp-config.php..

  • Tim Aych

    Excellent article! ST2′s incredible flexibility never fails to amaze me–I switched from Notepad++ several months ago and I’ve been extremely satisfied.

  • Rodrigo Passos

    Great Article!

    After playing with projects a bit, I hit a wall: I write a lot of ExpressionEngine templates, but they save as HTML. Every time I reopen a EE template I have to change the syntax to EE to highlight the tags.

    I could make the default of HTML to be EE syntax, but I only want that behavior on EE projects, not on my other project (Pure PHP or Codeigniter).

    I’ve tried this, with no success on my Project file: (Can you give me a hand?)

    {
    “folders”:
    [
    {
    "path": "/Fake/Path/To/It"
    }
    ],
    “settings”:
    {
    “tab_size”: 4,
    “ExpressionEngine”:
    {
    “extensions”:
    [
    "html"
    ]
    }
    }
    }

    Thanks!

  • Tam

    Thanks for article!!!

    I have a problem when i works with Sublime Text 2. In JSF, some tag just works when run on the server as Tomcat or JBoss. So, I want to automatic build my project with Sublime Text. How can i do that ???

    Finally, thanks for article again ^^

  • Slavcho

    Thanks a lot for the article. I learned new neat things.