Tag Archive: sublime

Posted 01.26.2015

Posted 01.26.2015

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:

Multiple Cursors

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.

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 + 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.

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.

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.)

Cmd + [

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

Cmd + ]

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

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.

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.

Ctrl + Shift + w

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

Cmd + Shift + .

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

Cmd + l

This will select the entire line your cursor is on.

Cmd + j

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

Ctrl + Shift + k

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

Cmd + Shift + K

This will highlight the wrapping tag.

Cmd + /

Will toggle a comment block on and off.

Cmd + Shift + <Enter>

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

Cmd + Enter

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

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.

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.

“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:

<!-- 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.

<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.

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

Alignment and AlignTab

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.

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.

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

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

It’s a comment, so you can’t see it on the front end.

It identifies the exact spot and line number in my code that still needs work.

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.

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?