Posted 01.26.2015

Posted 01.26.2015

A Brain Dump on Sublime Text 3

Sublime Text

Let’s face it, developers are opinionated about the tools we use. I’m no different.

In my humble opinion, Sublime Text is it. Dreamweaver and Coda have too many WYSIWYG features that are unnecessary to my workflow. I originally started coding with TextMate which I loved, however, the development seemed to stall out and Sublime offered several key features that TextMate was missing.

So, if you’re interested in Sublime or if you’re trying to trick it out, this post is for you! I figure if you’re going to spend all day with a tool, why not take the time to determine the best and most effective way you can use it. Let me share some of my workflow.

A Quick Word of Warning:

This post is L-O-N-G (it’s a brain dump!). To make it easier for you, I wanted to include a brief table of contents, in case you want to jump to a particular section:

  1. Features
  2. Keyboard Shortcuts
  3. Setting up your Preferences
  4. Snippets
  5. Plugins and Extensions
  6. Looking for More?
  7. Comments


Features

Some of my favorite features are actually native to the app:


Multiple Cursors

You can Cmd + Click in multiple places within your file to create multiple cursors or Alt + Drag Click to select multiple lines.

Sublime Text - Multiple Cursors


Multiple Panes

You can have multiple panes within Sublime.

Sublime Text - Multiple Panes

I prefer 2 columns, but you could have rows instead. — If this excites you, check out the Orgami plugin (also listed below).

I’ll usually keep my HTML on the left and my SCSS and JS on the right.

I’ve also found this feature useful, if I’m working with a longer file and constantly scrolling up and down. I can pull the top of the file up in one pane and put the bottom of the same file in another pane.


Search within a project

Cmd + Shift + F will open the project search pane. You can also limit your search to certain files and folders.

If you want to limit the search to the current project type <project> within the “Where” field.

Sublime Text - Search within prject

Ever since I started splitting my SASS into multiple files, this feature has become invaluable. Within the “where” field, I usually include “assets/src/scss/“ so that the results are only within my SASS directory. I don’t want the compiled .css returning.

Within the “Find Results” screen, you can double click on the file name and it will take you to that particular file — or you can click on the line of code and it will take you to that spot specifically.

Lastly, you’ll see buttons to the left of your search fields that allow to toggle regular expressions, case sensitive, whole word, show context, and use buffer. I only make use of show context and use buffer.

NOTE

You may want to look into folder_exclude_patterns within your preferences file if you want to omit certain results from your search.



Keyboard Shortcuts

Your options here are limitless. I don’t know about you, but I can only remember so many at a time. I’ve included the ones I’ve found to be the most useful that I find myself using on a daily basis.


Cmd + t

This is by far my most used shortcut.

This opens a panel at the top, type the name (or part of a name) of a file within your project, hit <ENTER> and it will open.

cmd_t


Cmd + Shift + p

This short cut runs a close second. Similar to Cmd + t, this will open a panel at the top of my screen. Except, instead of opening files, it allows you to run commands. This could be a command from a plugin or extension or a command you might find in the menus (but don’t want to use your mouse).


Cmd + r

This allows you to easily find and jump to a piece of code within the current file. Within a CSS or SASS file, you can easily find a class or id tag. Within a PHP file, you can easily find a function.


Cmd + Shift + t

This will re-open the last tab that you closed.


Cmd + 1

Cmd + 2

Cmd + 3 (through 9)

If you numbered your tabs, left to right starting with 1, hitting Cmd + 2, would allow you to jump to the second tab. Cmd + 4, the fourth. You get the idea.

Sublime Text - Cmd Number


Ctrl + 1

Ctrl + 2

If you have multiple panes open and numbered them left to right starting with 1, hitting Ctrl + 2 would allow you to jump to the second pane. Ctrl + 1 will jump back to the first pane.

If you’re using this shortcut in conjunction with Cmd + 1 (through 9), the tab numbers restart within each pane. For example, I might type Ctrl + 2 to jump to the second pane, and then Cmd + 3 to jump to the third tab within that pane.

Sublime Text - Ctrl + Number


Cmd + k + l

Cmd + k + u

The sequence, here, took me a while to “get.” Basically, keep the Cmd key held down the whole time, and then type k (release) l (or u).

This will transform your selected text to all uppercase or all lowercase. I can’t tell you how many times I’ve started typing with the CAPS lock on by accident. Being the anal retentive programmer that I am, instead of having to delete that text and retype, Cmd + k + l. (I remember it by thinking konvert to upper or lower.)

Sublime Text - Cmd + k + u


Cmd + [

This will indent your entire line of code (or selection) to left.

Sublime Text + Cmd + Left Bracket


Cmd + ]

Similar to Cmd + ], this will indent your entire line of code (or selection) to the right.

Sublime Text - Cmd Right Bracket


Cmd + Shift + v

This will paste and indent, automatically applying the correct indention to the pasted text.


Cmd + k + b

Slides the sidebar in and out. I use this more when I’m working remotely on my laptop and screen real estate is an issue.

Sublime Text - Command + K + B


Cmd + d

This will highlight the entire word that your cursor is in. If you hit it again, it will select the next instance that word is used.

Sublime Text - Cmd + D


Ctrl + Shift + w

I use this one all the time! It wraps your current selection with a tag.

Sublime Text - Ctrl + Shift + W


Cmd + Shift + .

This is another one that has become second nature for me. This will automatically complete an open tag.

Sublime Text - Cmd + Shift + .


Cmd + l

This will select the entire line your cursor is on.

Sublime Text - Cmd + L


Cmd + j

This will get rid of the return at the end of the line.

Sublime Text - Cmd + J


Ctrl + Shift + k

This will get rid of the entire line your cursor is on. Great for cleaning up code.

Sublime Text - Ctrl + Shift + K


Cmd + Shift + K

This will highlight the wrapping tag.

Sublime Text Shortcut - Cmd + K


Cmd + /

Will toggle a comment block on and off.

Sublime Text Shortcut - Cmd + /


Cmd + Shift + <Enter>

This will move your cursor to a new line above your current position.

Sublime Text Shortcut - Cmd + Shift + Enter


Cmd + Enter

Same thing as Cmd + Shift + <ENTER> except, it will move your cursor to a new line below your current position.

Sublime Text Shortcut - Cmd + Shift


Ctrl + Cmd + Up Arrow

Ctrl + Cmd + Down Arrow

This is great when you have a list you’re trying to reorder. Ctrl + Cmd + Up Arrow moves your current line up. Similarly, Ctrl + Cmd + Down Arrow moves your current line down.

Sublime Text Shortcut - Cmd + Ctrl + Arrow



Setting up Your Preferences

One of the “weird” things about Sublime is that the preferences file uses JSON. Admittedly, not the most user friendly.

Go to Sublime Text > Preferences > Settings – User to edit the file.

I’ve included mine:

Within my preference, I make the folder labels bold:

“bold_folder_labels”: true,

I use the Cobalt theme (goes back to my TextMate days).

"color_scheme": "Packages/Color Scheme - Default/Cobalt.tmTheme",

You can actually set that through the menus. Just go to Sublime Text > Preferences > Color Scheme > Color Scheme – Default > Cobalt

I’ve also experimented with Dayle Rees’s color schemes, if you’re looking for more options. There are instructions on his GitHub page on how to install.

Other things I’ve defined in my preference file?

"folder_exclude_patterns":

[

".svn",

".git",

".hg",

"CVS"

],

This prevents center file extensions from appearing in my sidebar and project search.

NOTE:

You can also set folder_exclude_patterns on a project level.

“font_size”: 14

Controls the font size. Surprise! :-)

“highlight_line”: true,

Highlights the line that your cursor is on.

“highlight_modified_tabs”: true

This setting changes the color of the file name in a tab if the file has been modified.

Sublime Text - Highlighted Tab

“spell_check”: true

Runs spell check.



Snippets

This is where Sublime starts to get really powerful and workflow efficient.

“Snippets” of code that I find myself typing all the time, I’ll turn into a Sublime Snippet. It’s as easy as going to Tools > New Snippet. Sublime will open a new file with a template:

<snippet>

<content><!CDATA[

Hello, ${1:this} is a ${2:snippet}.

]]></content>

<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->

<!-- <tabTrigger>hello</tabTrigger> -->

<!-- Optional: Set a scope to limit where the snippet will trigger -->

<!-- <scope>source.python</scope> -->

</snippet>

To understand snippets, let’s talk through the default snippet and how it would work. If we uncommented line 6 and saved this snippet as hello.sublime-snippet (all snippets must have a sublime-snippet extension), every time we typed “hello<TAB>”, Hello, this is a snippet. would appear. You’ll notice that this is highlighted. We could type whatever we want to replace “this”, hit <TAB> again and snippet would be highlighted. <TAB> again, and our cursor goes to the end of the line.

So what just happened? Well, on line 3 of our, between <content><![CDATA[ and ]]</content> is our code. The ${1:this} and ${2:snippet} signifies our tab stops.

Technically you don’t need the :this and :snippet, but they serve as placeholder values to remind you what content needs to go there.

The number tells Sublime what order to tab through.

If you want to get fancy, you can have multiple ${1:this}s in your snippets. That means that you’d have multiple cursors as you’re tabbing through. Let me give you a practical example:

This is my starting point for creating a JavaScript controller file. You’ll see two ${1}s, two ${2}s, and two ${3}s.

When I type “start_js<TAB>” my code appears with my cursor in two places. I can type the name of my variable on line 5. I’ll know I’ll initialize it on line 6 using the same name. Why not do it all at once? I hit <TAB> and I have cursor on line 5 and another on line 10. I can create a new instance of my object (line 5) and define the name of my object (line 10) at the same time… you get the idea.

<tabTrigger>hello</tabTrigger>

This is the text you’ll type (followed by the <TAB> key) to trigger the snippet. By default, this line is commented out, so make sure to name it appropriately and uncomment that line before saving it.

— It’s OK if you forget the tab trigger. Type Cmd + Shift + P. You’ll see a bar appear, type “snippet”, and then as you continue to type, Sublime will drill down and try to find the snippet you’re referring to. It will also remind you of the tab trigger on the far right.

Sublime Text Shortcut - Cmd + Shift + P

<scope>source.python</scope>

This line will help scope your triggers. What does that mean?Well, in the case of my JavaScript starter file, I’ve limited it to only working with JavaScript files. Instead of “.python” it says “.js”. Any file I want to use that snippet in, must be saved with a .js extension.

If I have trouble with a snippet loading, it’s usually related to the scope.

By default this line is commented out, so it’s not necessary, but it prevents your snippets from conflicting with each other.


Tips and Tricks

When you’re saving your snippets, they’ll be placed in your User’s Library folder > Application Support > Sublime Text 3 > Packages > User >. From there, though, I like to keep my snippets organized. I have folders for each of the languages (CSS, HTML, JS, and PHP).


Other Snippets

If you’re curious what other snippets I use, I’ve created gists that you’re more than welcome to download and use.


CSS

HTML

JS

WordPress (PHP)



Plugins and Extensions

At the end of the day, the plugins and extensions are ultimately what tie me to Sublime. I’ve become so reliant on them that it would be really hard for me to change editors.


Package Control

The first thing you’ll need to do is install Package Controll. This makes it super simple to find and install packages and keep them up to date.

If you go to the Package Control install page they do a good job of explaining how to get started:

  • Go to Sublime, hit ctrl + ` (it’s above the <TAB> key).
  • Copy and paste the code they provide on their site and hit <ENTER>

Easy Peasy Lemon Squeezey.


Installing Packages

Now that Package Control is installed, anytime you want to install a package, just hit Cmd + Shift + P and then start to type “install”. Package Control: Install Package shoud come up as an option. Hit <ENTER>. Then, you can search for whatever package you want to install. Select it. Hit <ENTER> and it will automagically download and install itself and stay up to date. You can watch the progress in the bottom left gray bar.


Advanced New File

Advanced New File makes it easy to create new files and folders from your keyboard. I simply type Cmd + alt + shift + n, a line appear at the bottom of window and I can type the name of the file I want to create. If I want it to be within a particular folder, I simply include the path as well: assets/src/scss/_1_base/globals.scss. If I want to create a folder, I still type Cmd + alt + shift + n, but instead of typing the file name, I type the folder name. It knows the difference because instead of ending in a dot extension, end with a trailing /. For example: assets/src/img/svg/.

By default, when you create a new file, it will add a __init__.py to your folder. This is great, if you’re writing python (I’m assuming, I don’t know python). You can turn this off by going to Sublime Text > Preferences > Package Settings > AdvancedNewFile > Key Bindings — User

Copy and paste the following line:

[
	{ "keys": ["shift+super+alt+n"], "command": "advanced_new_file_new", "args": {"is_python": false}},
]

Anything within your Key Bindings — User file, will override the defaults established within Key Bindings — Default.


Alignment and AlignTab

The Alignment and AlignTab make it easy to correct alignment within your code.

With the Alignment package, you can press Cmd + Ctrl + a and your selection will indent consistently. If the lines are already indented properly, it will then check to make sure the first = on each line is aligned.

The AlignTab package is a little more versatile. I will typically call it by typing Cmd + Shift + P then selecting AlignTab and <ENTER>. A input field will appear at the bottom of your window and your type the character you want the text to align by.

It’s also worth noting that AlignTab has a Live Preview Mode option that allows you to preview the change before hitting <ENTER>.


Auto File Name

AutoFileName will autocomplete the folder and file names as you’re typing. So handy! If you’re referencing an image source, it will also fill in the height and width of the image.

Sublime Text - Autofile Plugin

NOTE: Within the screenshot, I’m using Emmet to generate to <img> tag.

Bracket Highlighter

BracketHighlighter will highlight the matching tag, plus it will put an icon in the gutter to help you identify the bracket, parenthesis, tag, etc.

Sublime Text = Bracket Highlighter

This has been a tremendous help in helping me troubleshoot code, when my nesting gets off.


Comment Snippets

Comment Snippets is a set of snippets that help you build comment blocks. For example, comm + tab would render

/* Comment */

Or, comm-section + tab would produce:

/*=============================================
    =            Section comment block            =
    =============================================*/



    /*-----  End of Section comment block  ------*/

You can see more examples by reading their documentation.


DocBlockr

DocBlockr is similar to Comment Snippets in that it helps create comments, but it’s more robust. For example, if you’re writing a comment block, it will maintain your style and indentation.

Sublime Text - DocBlockr Example

Also, if you’re going back and documenting variables and functions, DocBlockr will pick up on your variables and parameters, pre-filling your comment.
Sublime Text - DocBlockr example

It can also do fancy things, like surrounding you content with slashes. // SomethingCTRL + <ENTER>:

Sublime Text - DocBlockr Example

This is just the tip of the iceberg.


Syntax

You can extend Sublime to support different languages and syntax highlighting through plugins too. There’s one for just about anything you can imagine:

You get the idea.


Emmet

If you don’t install any other packages, install this one! I use Emmet (html) and Emmet CSS Snippets and Emmet Style Reflector.

It makes coding SO MUCH easier. Just to give you an idea, you can type ul>li*3>a[href=#] and then hit tab and it will generate this for you:

<ul>
<li><a href=“#”></a></li>
<li><a href=“#”></a></li>
<li><a href=“#”></a></li>
</ul>

The > sets up the nesting, the * sets up how many elements, and the [] establishes any attributes.

The CSS version is somewhat similar. I can just start to type (typically) the first two letters of each word in a property, hit <TAB> and it will generate that property for me.

For example ff <TAB> will produce font-family: ; or td <TAB> will give me text-decoration: ;.

Chris Coyier has a 20 minute screencast on how to use Emmet (embedded below). I can’t tell you how many hours Emmet has saved me, it’s more than worth taking 20 minutes to familiarize yourself.


GitGutter

If you’re running .git on your project (which you should! …no excuses!), then GitGutter will put an icon in the gutter, next to the line number, letting you know what has changed since your last commit.

…and while we’re talking about Git, you may want to check out Git and GitHub Tools. Git provides git integration directly in Sublime. (I still use Tower, though.) GitHub tools provides GitHub integration (I’ve moved most of my code to BitBucket.)


HTML-CSS-JS Prettify

Admittedly, this is not the most used plugin that I have installed. But, when I do use it, it’s like magic.

HTML-CSS-JS Prettify will take an HTML, CSS, or JS file and make the code look really pretty. It comes in handy if I copied the source from somewhere or if I’m dealing with someone else’s code.


Open Finder

Open Finder gives you the command “Finder: Open here”, which will open the current file’s folder in Finder. Sounds simple, but very useful.


Origami

This is one of my favorites. It makes opening and closing panes so easy. I simply type Cmd + K + arrow to open a new pane. Arrow up, opens a pane above. Arrow down, one below. Arrow right, to the right. Arrow left, to the left.

If I want to close a pane, I simply type Cmd + K + Shift + arrow. (Be careful not to get this mixed up with Cmd + Shift + K, just remember hold down the command key, press and release the k button, then hit shift, then arrow) Arrow up, closes the pane above it. etc.


Placeholders

Placeholders provides placeholder content. So, I can simply type lorem<TAB> and a paragraph of Lorem Ipsum will be generated.


Sidebar Enhancements

SideBarEnhacements provides some additional functionality to your sidebar. For example, it provides some additional menu options when you right click on a file or folder in the sidebar (“move to trash” and “open with…”) just to name a few.


Sublime GitHub

Sublime GitHub is a must for me. It allows me to access my gists directly from Sublime. We talked about snippets earlier and I suppose gists could provide some overlap. The difference, for me, though, is a snippet requires some interraction (tabbing through). A gist, however, could be a whole piece of code or an entire file that I reuse quite frequently. My bower.json, package.json, or gulpfile.js are perfect examples.

Within Sublime, I will type Cmd + Shift + P, then I’ll start to type gist. I’ll select GitHub: Copy Gist to Clipboard and hit <ENTER>. Within the top panel, it will list all my gists. I can start to type the name of the gist, select it with my arrow keys if it’s not already selected, hit <ENTER> and then Cmd + V to paste it into my current file.


ToDo Review

Last but not least: ToDoReview. As I’m typing code, sometime I’ll write a line, but know that I need to come back and work on it. Say I’m stubbing out a page and have a placeholder for a JavaScript slider. I know I’ll need to come back and work on the slider, but I don’t have time in the moment to implement it. So, I’ll type <!-- TODO: Implement Slider -->

The great thing about this method

  1. It’s a comment, so you can’t see it on the front end.
  2. It identifies the exact spot and line number in my code that still needs work.
  3. It doesn’t interrupt my flow of stubbing out the page.

Later, I can go back and run TodoReview: Project Files (by hitting Cmd + Shift + P and it will generate a list of all the places in the code where I listed TODO items. That list then comes my checklist. I can double click on the TODO in the list and it will take me to that spot in the code. Once I’ve reworked it, I’ll remove the comment, and regenerate the list.

NOTE:

You may notice, if you’re implementing bower or other node components, this syntax is common among other developers. By running TodoReview: Project Files, it will return Todo items within your project’s bower_components folder and node_modules folder that other programmers have left behind.

You can exclude these fles folders from your results, by going to Sublime Text > Preferences > Package Settings > ToDoReview > Settings — User and listing them within your preferences.json file. I’ve included mine for reference:



Looking for more?

Whew! That’s all I got. If I’ve just whetted your appetite or you’re looking for more information, here are a few additional resources:



I know this seems like a lot, but I know Sublime is so powerful and extendable that there’s plenty more. I’m interested to hear in the comments, what are some of your tips and tricks? Snippets? Plugins? Macros?