Sublime Text 2 Tips and Tricks (Updated)

Sublime Text 2 Tips and Tricks (Updated)

Tutorial Details
  • Editor: Sublime 2
  • Availability: OSX, Linux, Windows

Sublime Text 2 is one of the fastest and most incredible code editors to be released in a long time! With a community and plugin ecosystem as passionate as this one, it just might be impossible for any other editor to catch up. I’ll show you my favorite tips and tricks today.

Sublime Text 2 is currently available for all major platforms: OS X, Linux and Windows.


1 – Bleeding Edge Versions

Sublime is in active development. If, like me, you want to use the latest possible version of the app, you can download the dev build. You’ll find that new (auto) updates are available every other day or so.

Download a dev build of Sublime 2 here.


2 – Get a Better Icon

Update: here is a better icon.

In its defense, Sublime Text 2 is still in a beta state. The official icon will likely/hopefully change with the official release. Until then, Nate Beaty created an alternative icon, if you prefer it.

To integrate it, you need to replace the existing “Sublime Text 2.icns” file with this new one. On a Mac, browse to Sublime 2 in your Applications/ folder, then right-click and “View Package Contents.” Lastly, browse to Contents/Resources/, and drag the new icon in, overwriting the existing one.

Please take note of the fact that, if you’re using the frequently updated development version of Sublime Text, with each update, the icon will be removed. With that in mind, don’t worry about the icon for the time being.


3 – Access the Command Palette

Similar to TextMate, we can use Sublime’s command palette by accessing the Tools menu, or by pressing Shift + Command + P, on the Mac. Whether you need to visit a Preferences page, or paste in a snippet, all of that can be accomplished here.

Access the command palette

4 – Lightning-Fast File Switching

File Switching

Press Control or Command + P, type in the name of the file you wish to access (fuzzy finder), and, without even pressing Enter, you’ll instantly be transported to that file. While Vim and apps like PeepOpen offer a similar functionality, they’re not nearly as fast as Sublime’s implementation.


5 – How Did We Survive Before Multi-Selection?

Editors like TextMate have long offered vertical selection, which is quite neat. But, with multi-selection, you can have multiple cursors on the page. This can drastically reduce the need for using regular expressions, and advanced search and replace queries. Perhaps a quick visual demonstration is in order…

To enable multi-selection, you have several options:

  • Press Alt or Command and then click in each region where you require a cursor.
  • Select a block of lines, and then press Shift + Command + L.
  • Place the cursor over a particular word, and press Control/Command + D repeatedly to select additional occurrences of that word.
  • Alternatively, add an additional cursor at all occurrences of a word by typing Alt+F3 on Windows, or Ctrl+Command+G on the Mac. Amazing!!

6 – Indent Guides

Update: this feature now comes preinstalled with Sublime Text 2.

It’s such a small feature, but I’ve always loved how Notepad++ on Windows displays indent guides; it makes the page much easier to navigate and format. Sublime Text 2 offers this ability, via a plugin created by Nikolaus Wittenstein.

Indent Guides

To integrate this plugin:

  • Download it
  • Rename the folder to “Indent Guides” and drag it into the Packages folder. On a Mac, this path would be Application Support/Sublime Text 2/Packages

7 – Package Control

The steps outlined in the previous tip (#6) are a bit tedious, aren’t they? Instead, we can install the excellent Sublime Package Control, which streamlines the entire process.

To install “Package Control,” open Sublime and press Control + `. Next, paste the following snippet into the console.

 import urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp) if not os.path.exists(ipp) else None;open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read())

Don’t worry if you don’t understand the code above; just copy and paste!

Console

Lastly, restart Sublime Text, and browse to Preferences -> Package Settings. If the installation was successful, you’ll now see a Package Control item in that list.

With Package Control installed, the process of adding new plugins and functionality becomes incredibly simple!

For a usage example, refer to the next item in this list.


8 – Alignment

If you’re the type who prefers to line up your equal signs – for example, in your JavaScript…

// Before
var joe = 'joe';
var johnny = 'johnny';
var quaid = 'quaid';

// After
var joe    = 'joe';
var johnny = 'johnny';
var quaid  = 'quaid';

…this process can be automated, via the Sublime Alignment plugin. Rather than downloading and installing it manually, let’s instead use Package Control (outlined in #7).

  • Press Shift + Command + P
  • Type “install,” to bring up the “Package Control: Install Package” option, and press Enter
  • Look for “Alignment,” and press Enter to install it.
  • You’re done; so easy! Type Shift + Command + A to auto-align.
Auto-align

This process can be repeated for all of the typical plugins we install, such as Zen Coding.


9 – Vim Fanatic

I’m a huge fan of Vim. The amount of power it provides is insane. The fact that I’ve switched over to Sublime Text 2 should speak volumes then!

If you’re using a dev build of Sublime Text (see #2 in this list), you can enable Vintage mode, which provides support for the Vi commands that we know and love — okay…some of us love. The rest of you hate it! :)

To enable Vintage mode, browse to Preferences/Global Settings - Default. Once this file opens, browse to the very bottom, and change "ignored_packages": ["Vintage"] to "ignored_packages": []. Next, restart Sublime, press the Escape key, and, tada: command mode!

Block Cursor

One thing you may notice is that, in command mode, it can be difficult to find the cursor (especially when taking advantage of things like bookmarks). On more than one occasion, I’ve found myself trying to hunt down its location.

While it’s not a perfect solution, a plugin, called “SublimeBlockCursor,” attempts to remedy this issue.

Note: While the readme states that SublimeBlockCursor can be installed, via Package Control, I wasn’t able to find it. Instead, I had to clone the project manually into the Packages folder.


10 – Distraction Free Editing

Sometimes, we need to filter out all of the additional fluff that gets in the way of our coding. Use “Distraction Free Mode” to take this idea as far as possible. This option is available, via the View menu. Select “Enter Distraction Free Mode,” or use the Mac keyboard shortcut, Control + Shift + Command + F.

Distraction Free Mode

11 – You Can Still Use TextMate Bundles

Slim

TextMate snippets and themes port over nicely to Sublime Text. You only need to drop them in the Packages folder — .tmbundle extension intact, and Sublime will recognize the files. This means that the entire catalog of TextMate themes will work in Sublime!

For example, I’ve been working with the (fantastic) Slim templating engine a good bit lately, and needed better syntax highlighting. Fred Wu created a bundle for TextMate, but, tada, it works perfectly in Sublime Text as well! If you’re interested, you can download the Slim bundle here; it includes both snippets and syntax highlighting.


12 – Custom Themes

The default theme for Sublime Text is excellent, but I much prefer a custom light and dark theme, Soda, created by Ian Hill.

Soda Theme

Installation

As taken from the Github page…

“If you are a git user, the best way to install the theme and keep up to date is to clone the repo directly into your Packages directory in the Sublime Text 2 application settings area.”

Using Git

Go to your Sublime Text 2 Packages directory and clone the theme repository using the command below:

 git clone https://github.com/buymeasoda/soda-theme/ "Theme - Soda"

Download Manually

  • Download the files using the GitHub .zip download option.
  • Unzip the files and rename the folder to Theme – Soda
  • Copy the folder to your Sublime Text 2 Packages directory

Activating the Theme

To configure Sublime Text 2 to use the theme:

  • Open your Sublime Text 2 User Global Preferences file: Sublime Text 2 -> Preferences -> User Global Settings
  • Add (or update) your theme entry to be “theme”: “Soda Light.sublime-theme” or “theme”: “Soda Dark.sublime-theme”

Example User Global Settings

{
    "theme": "Soda Light.sublime-theme"
}

13 – Page Crawling

Sublime Text provides us with a few different ways to query a page (outside of the standard search functions).

Functions

Need a quick way to browse to a specific function or method?

Function Crawling

Type Control/Command + r to reveal a popup that allows for this very thing (notice the @ symbol)! Even better, the search is fuzzy as well, which is particularly helpful for huge classes.

HTML

What if you want to immediately transition to a specific part of an HTML page – say, to the div with a class of container. Type Control/Command + p, then #, and you’ll instantly see a tree of your document.

Go to Line Number

To quickly move to a specific line number on the page, you can press Control + g. However, you’ll notice that, once again, it’s pulling up that palette (Control/Command + p), and appending the : symbol. This is adopted from Vim.

This means, in addition to Control + g, you can also type, Control/Command + p, and then :LINE_NUMBER.


14 – Fetch Remote Files With Ease

Nettuts+ Fetch

Let’s say that you’re a fan of Normalize.css. Perhaps, you download it and save it to a snippet, or store the stylesheet, itself, in an assets folder. That way, for future projects, you only need to copy and paste.

The only problem with this method – as we’ve all discovered – is that, if a few months have passed, it’s more than possible that the asset (in this case, Normalize.css) will have been updated by the creator. So your options are to either use the, now, out-dated version of Normalize, or, once again, return to the GitHub page and pull in a fresh copy. This all seems tedious.

Created by Weslly Honorato, Nettuts+ Fetch is the solution to our dilemma. It can be installed, via Package Control.

Usage

You’ll only use two commands, when working with Fetch. First, we need to save some file references. Again, bring up the command palette, and search for “Fetch.” For now, choose “Manage Remote Files.”

Manage Remote Files

What’s great about Sublime Text 2 is that configuration is incredibly simple. To assign references to online asset files, we only need to create an object, like so (don’t worry; one will be pre-populated for you, after installation):

So, to pull in the latest copy of jQuery (if you don’t want to use a CDN):

{
	"files":
	{
		"jquery": "http://code.jquery.com/jquery.min.js"
	}
}

Learn more about using Nettuts+ Fetch.


15 – Prefixr Plugin

Built by by Will Bond (creator of Package Control), the Nettuts+ Prefixr plugin allows you to automatically update your entire stylesheet to include support for all of the various required CSS3 vendor prefixes. This way, you never have to visit the website itself; you merely type a keyboard command, and:

.box {
   border-radius: 10px;
   box-shadow: 0 0 5px rgba(0,0,0,.4);
}

…will be converted to:

.box {
	-webkit-border-radius: 10px;
	-moz-border-radius: 10px;
	border-radius: 10px;

	-webkit-box-shadow: 0 0 5px rgba(0,0,0,.4);
	-moz-box-shadow: 0 0 5px rgba(0,0,0,.4);
	box-shadow: 0 0 5px rgba(0,0,0,.4);
}

Usage

Once installed (via Package Control), select your stylesheet (or a single block), press ctrl+alt+x on Windows and Linux, or cmd+ctrl+x on OS X, and the code will instantly be run through the Prefixr web service.


16 – Launch Sublime From the Terminal

Sublime Text 2 includes a command line tool, subl, to work with files on the command line.”

To use it, create a symlink to the tool.

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

As long as ~/bin is in your path, that should do the trick!

cd myProject
subl .

Refer here for additional instructions.


17 – Autoformat HTML

A bit oddly, the ability to auto-format HTML is not included as part of the default build of Sublime Text. The Tag plugin, among other things, hopes to provide a solution, however, due to a few shortcomings – namely when dealing with HTML comments – it falls short.

The Tag plugin can be installed via Package Control.

To test its auto-formatting skills, the following HTML:

<html>
<head>
	<meta charset=utf8>
	<title></title>
	<!-- begin-->

	<!-- end comment -->
</head>
<body>

<h1>Hi there</h1>

<h2>Bye</h2>
</body>
</html>

…will be changed to:

<html>
	<head>
		<meta charset=utf8>
		<title></title><!-- begin-->
<!-- end comment -->
	</head>
	<body><h1>Hi there</h1>

		<h2>Bye</h2>
	</body>
</html>

Yikes; it looks worse than it is. From my tests, it incorrectly does not place the body tag on its own line, and gets pissy when dealing with HTML comments. Until these issues are fixed (or a native solution is provided), it’s best to manually select a block of HTML to reformat, rather than the entire page.

A $200 bonus will be paid to the first Sublime Text plugin developer who creates and submits the definitive “Nettuts+ HTML Formatter” plugin.


18 – Create a Plugin

If you’re feeling adventurous, dig into Sublime Text’s huge plugin development community, and start contributing. We have an excellent tutorial on the process of building a ST plugin here on Nettuts+. Be sure to check it out if that interests you!


Conclusion

The more I work with Sublime Text 2, the more I realize how incredible it is. But all of this would mean nothing if not for the fact that it’s an insanely fast editor, and, even better, it’s not finished!

Add Comment

Discussion 297 Comments

Comment Page 4 of 4 1 2 3 4
  1. Axel Grazx says:

    Hi! Today is my second day using Sublime, and it fells great… I’m also a huge fan of development in Dreamweaver and there is a feature in special that I wonder if is available in Sublime: the class/id auto-complete…

    It get all the CSS’s classes and id’s and when you’re about to use it (ex. <div class="…) give you a list of the ones of the css file attached.

    Sublime can do it?

    • In all honesty, you should be developing the other way around: you set tags in HTML and *then* style them in CSS…

      • Simon says:

        What Alex Grazx is asking for makes sense to me. It has to do with keeping your code DRY. You don’t want to individually style every element, you would want to reuse styles from your existing style sheet.

  2. peter says:

    Hi! I have a question about creating new files in ST2 like Jeff on his “Jquery in 30 days” course in lesson 4. You make right click on the folder name in sidebar and choose “New file” and then input (at the bottom) where you can type a name of new file appears. I’m curious is it plugin or just a setting? It’s really handy and I want it to set up in my editor. Thanks in advance!

    • Johannes says:

      I’d really like to know that too. I have messed around with Sublime’s settings and I got really basic and pretty useless sidebar, you can change between open files and close them. I have done some Google research on that but nothing so far. Does someone know how to get that result?

      • webwesen says:

        add a folder to you project – it will show up in the sidebar (Project -> Add Folder…)

      • Michiel says:

        I think you mean the SideBarEnhancements plugin.

      • peter says:

        I managed to find a plugin and this is SideBarEnhancements like Michiel said. It’s great and give a lot of options i.e. Open With (you can set up any app i.e. Firefox or Chrome).

        But it would be much better if this worked with CTRL+N instead of Right Click -> New file, but it’s very handy anyway ;)

      • ZeljkoSI says:

        Like webwesen said, add a project folder and you can create new files with right click -> new, you don’t need a sidebar plugin.

      • e_dub says:

        Hey guys, there’s a killer package called Quick File Creator that does what I believe you guys are looking for, and what’s more, it also gives you the ability to quickly add whole Folders as well.

    • e_dub says:

      Forgot to mention , it has keyboard shortcuts for this: ctrl + super + n, and ctrl +super + d respectively, though you might have to add these to your user keybindings by hand. The read me provides the correct formatting, just add cut and paste them from the read-me file into the user-keybindings file (available from the preferences menu). Make sure to place them between brackets, and that there is a comma between each keybinding you add (but no comma after the last one).

      In fact, a good tip, open up the package browser, go through your packages and read the read-me files. MANY of them have additional features/options you can set, or keybindings you can create, adding a great deal of function to ST2. Just be sure to create a cheat-sheet file and keep track of any shortcuts you create. This way you don’t add/learn of a bunch only to promptly forget them, and have no reference to go back to when you need them.

  3. Bill says:

    @Axel Grazx

    ctrl+space works for the linked css, but not just css unfortunately. So if you know the first letter or two for the class or id it works alright then.

    Still learning how to use Sublime myself. :)

  4. It doesn’t even have the code autocompletition for PHP (ctrl+space). I run the program for about 5 minutes. I think that it’s the same as Notepad++.

  5. Igor says:

    Beautiful, but inefficient for PHP (from my point of view). No autocompletion.

    • e_dub says:

      There’s plugins for that. You need to check out the package manager as it suggests in the article, and look through the tons of available plugins. There are legions of them for PHP.

  6. Juan says:

    Hi there!

    I have a question about how to turn off parenthesis matching for editing clojure file???

    eachtime I type ( will become ()

    which is good for text editing. But annoying for editing clojure files.

    Thanks a lot!

  7. Ashfaq says:

    hi

    can you repeat step 7 for control package, coz i didn’t get what to press with contro [control + ?] plz

    • Gowtham says:

      @Ashfaq The keybind for launching Python console is “Ctrl + `” (Control + Backquote). After you open the console, copy-paste the python code in the interactive console.

  8. Juan says:

    Anyone know what happened to vertical selection? Since the latest beta update, I haven’t been able to select text vertically on my macbook, Alt+click just pops up the right click menu. At least, I think it was alt-click. No matter what key combo I tried, I couldn’t get vertical selection to work. (Keep in mind, I’m not referring to clicking and dragging down the gutter and selecting entire lines.)

  9. Nicolas says:

    I’m using Vintage mode on OS X 1.07 and pressing & holding the “j” key only go down one line.. it does not keep going.. Anyone know how to change that ?

  10. kilinkis says:

    my mate at the office been using it for a while, now im trying it out, i really love that it has the VIM keys (L) xD!

  11. Rajat says:

    I’m using sublme text 2 build 2181 I want to know if there is plugin which autocapitalse first letter of words.Please let me know

  12. Patrice says:

    I just tried the Tag package, and it didn’t manage to format my HTML source correctly.
    It doesn’t seem to like nested tables that much.

    Other than that, I’ve been using Sublime Text for some weeks and don’t plan on using any other editor, I really love it.

    • Patrice says:

      I must confess, there was a mistake I did not see in my source, the Tag plugin is innocent and works fine.

  13. Colin says:

    Great stuff. Thank you!

  14. Great Software and Great Article!!! :-)

    I just want to report a mistake on the 5th trick. You wrote:

    «Press Alt or Command and then click in each region where you require a cursor.»

    But for Windows users is CTRL, not Alt!!!

    Hiiiiii!

  15. mystrdat says:

    #7, #16 – I smell the classy Pimpzilla scrollbars. Well played, Jeffrey. Well played indeed.

  16. Ken says:

    Url handler for xdebug file link in ubuntu
    https://gist.github.com/2037205

  17. Very powerful editor and excellent UI workspace, thanks for the tips and tricks!!

  18. juan says:

    Hi, thank you for great tutorial…

    I have a question on snippet.

    I create my own snippet. Called group.sublime-snippet save in where it should resides.


    grep

    source.rb

    I set the scope to .rb files, but the trigger (type grep, press tab) does not work in any .rb files. (I’ve restarted my sublime text2).

    Any tips??

  19. Abdel says:

    Hi there,

    After watching JQuery in 30 days Jeffrey way’s videos I just liked the Sublime Text 2 the way he used it and how fast it reacted.

    So quickly I got it, installed it, and started to struggle! spent hours now trying to figure out how can I set it up exactly like his on the video…apparently it’s worse than Coda without third party plugins! Alignment doesn’t work with shortcut..zenCoding doesn’t work either..I installed it so many times and there is absolutely no documentation of how to use it with ST2! forum is very weak also..

    it is definitely NOT out of the box editor for that price!

    I noticed also many limitations and bugs i.e the auto complete is limited to work with a file format, which means if you are coding in HTML it works, but as soon as you type inline CSS code it doesn’t trigger the auto-complete!

    Guys if I am missing something please explain and advise me! otherwise I am still and will be convinced that Dreamweaver is the most reliable code editor out there.

    Thanks
    A

    • Andrew says:

      Take a look at your computer processes and RAM usage when you have Dreamweaver open, and then take a look at it with Sublime Text 2 open. You should probably see a significant difference, and all your doing is editing text. Dreamweaver comes with MOUNTAINS of crap you don’t need. I use Coda, and the new one is less than great. Sublime seems much more barebones, not as user friendly right away, but once you write more, most people just want the editor to get out of the way and be lightning fast – which Sublime is and Dreamweaver is not.

  20. very good
    tips and tricks

    but there is a problem in the software witch i download it from sublimtext.com
    for windows 32

  21. Asif says:

    I love comment to tutorial

  22. netpro2k says:

    Just a quick note, I have fixed the issue causing SublimeBlockCursor not to show in Package Control. You should now be able to install it directly from within sublimetext.

  23. Jon says:

    I’ve just downloaded ST2 and I have to say all these features seem great.

    I installed Package Control and then attempted to install the Sublime Block Cursor plugin. It’s installed and I can see it in my folders, but I’m not seeing any block cursor in the editor? I’ve restarted the app but still nothing.

    Am i missing any steps?

  24. Siros says:

    Nice tips and Great Editor !

    Vinage Mode is getting better and better as more commands are added.

    by the way , I keep getting back to Vim after 5 mins of using ST2.

    And I don’t think that any one would leave vim , unless he wasn’t comfortable with it.

  25. Mohammed says:

    Perfect App!
    But I wanted only more feature:
    displaying multiple code coloring at the same time, for example, HTML with CSS & JavaScript would be perfect.
    Others may find PHP & HTML more important.
    Any way, this addition will make me very satisfied with this app.

  26. How good is the Vim support ?

    I mean, Vim accepts a lot of commands. Is there any limitation in Vintage or does it allow all commands I would be able to type on Vim?

    Thanks.

  27. please help me to find shortcut/plugin to browse the local dir. for example if img[src=] should open a dialogue to select image.

  28. Santhosh says:

    A quick update: the shortcut for Alignment is Control+Command+A as opposed to Shift+Commad+A (as mentioned in point 8).

    Thank you for a great article.

Comment Page 4 of 4 1 2 3 4

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.