Blog logoMatthew Lang

Sublime Text

A 3 post collection


Offline Tools

 •  Filed under Apps, Writing, Sublime Text, Marked, Byword

When we talk about tools that make us productive, we often refer to products and services that automate things for us. They do the leg work for a specific task while we move on with something else. While this is a nice idea in theory, too often than not, we find ourselves swept up by emails, messages, phone calls, social networks and other digital interruptions rather than moving on with that other task.

The best tools aren't just tools that automate work for us (although they do help), they also let us do with the work without interruptions. These tools don't rely on network connections, the Internet or any other digital highway to work. They just work, with or without an Internet connection. A few examples of tools spring to mind. For me tools like, Byword, Marked 2, Vim and Sublime Text are great tools for working offline, but being able to work offline aren't the biggest selling feature.

As a software engineer I do a lot of reading of manuals, technical and programmming books. I also read a lot of programming language and framework documentation through the day. My memory isn't as sharp as it used to be, so I might refer to the docs for a programming language or framework when I'm writing code. Most of these docs are available online. I'm not always online though. Which is why I love using Dash. It's a document browser that allows to the documentation of various programming languages and frameworks offline. So whether I'm online or not, I can always be sure to get access to the documentation I need when I'm working.

There's a huge number of apps now that try to keep us in a state of being permanently connected to the Internet, but it comes at a cost. Being online means being connected, being connected means being distracted and being distracted is how we fail do the work we intended to do. When it comes to being productive, look for tools that work offline. Turn off your wi-fi and get working. It's surprising how much work you get done.

Getting to Know Projects in Sublime Text

 •  Filed under Sublime Text, Programming, By Me

When I started using Sublime Text 2, I didn't use Sublime's projects feature. This year though, I've started to really get to know my preferred text editor and since then I now save all my code as a project in Sublime. It really does have some great advantages and with a little time it can make working in Sublime a better experience for you.

Sublime's projects are typical of projects in other text editors and IDE's. You open the folder that contains your source code with Sublime and then you can save that source code as a project.

When you save your code as a project you end up with two files. The first is the project file which contains references to folders for your project, project based settings and build commands for your project. The second file is the workspace. This is simply a file that tracks what layout you're currently using and what files you have open in each pane. Using the workspace file means that you can switch to another project, do some work and then switchback to your original project knowing that the layout and files you had open will be restored back to the state you left them in. Handy.

Opening Projects

Let's start with opening projects. You can open a project from the command line by using the project switch from Sublime's executable.

>> subl --project deathstar.sublime-project

Nice, but a tad too much to type. Rather than keying this out when I need to open Sublime, I prefer to alias the opening of a project file into a command that I can remember.

>> sds

Typing these three letters into my terminal to open a project is much easier than trying to remember where the project is and the correct switch for opening a project in Sublime. Now that we have our project open we can start tweaking the project file itself to make suit our needs.

There are three sections to the project file:

  1. Folders - You can define a single location for your project or multiple folders that make up a project. This also include filters on files and folders you might want to apply to each folder in this section.
  2. Settings - The settings in the editor can be changed on a project basis. If a particular language for your project requires different settings, e.g. tab size, you can define these here and the changes will take affect when you open the project.
  3. Build Systems - I tend not to use this, but you can keep a number of different terminal commands here that you can tell Sublime to execute without having to switch to the terminal.

Using Folders

Let's take a look at the most important section which is folders. Although this section is only small it can make a big difference to the way you work with your project and with Sublime.

The project file is just JSON and is fairly easy to follow even if you don't have that much experience with JSON.

{
  "folders":
  [
    {
      "path": "/Users/darthvader/code/deathstar-reactor"
    }
  ]
}

The path setting points to the folder that contains the files for your project. Most of the time you might just have one instance of this in your project file, but Sublime does allow you to have more than one folder in your project file.

{
  "folders":
  [
    {
      "path": "/Users/darthvader/code/deathstar-reactor"
    },
    {
      "path": "/Users/darthvader/code/deathstar-superlaser"
    }
  ]
}

I've been using multiple folders for a couple of projects now. I'm rewriting an application just now that uses multiple folders. For that project I included the old source code and the new source code in the same project so that I can refer back to the old code to lookup any old code.

Which leads us nicely onto names. Having multiple folders in your project can be confusing, especially when projects might have similar folder names or even the same name. To get round this, you can also define a name for each path in your project that will appear in the sidebar. This makes navigating code in your sidebar much easier.

{
  "folders":
  [
    {
      "name": "DeathStar - New & Improved Reactor",
      "path": "/Users/darthvader/code/deathstar-reactor"
    },
    {
      "name": "DeathStar - Superlaser x10",
      "path": "/Users/darthvader/code/deathstar-superlaser"
    }
  ]
}

Perhaps the most useful feature of the projects file though is the ability to exclude files and folders from your project. You are not going to need to see all the files and folders in Sublime when you are coding, so these filters are a great for excluding logs, temp files and other automatically generated files that are not typically needed in Sublime.

Excluding files can be done like this:

{
  "folders":
  [
    {
      "name": "DeathStar - New & Improved Reactor",
      "path": "/Users/darthvader/code/deathstar-reactor",
      "file_exclude_patterns": [
        "*.log",
        "*.pid",
        "*.tmp"
      ]
    }
  ]
}

And excluding folders can be done like this:

{
  "folders":
  [
    {
      "name": "DeathStar - New & Improved Reactor",
      "path": "/Users/darthvader/code/deathstar-reactor",
      "folder_exclude_patterns": [
        "tmp",
        "log",
        "solr"
      ]
    }
  ]
}

Switching Projects

Now that we have our project file setup we can get on with using it.

Because I now have a projects file for each project I work on in Sublime, I find it much easier now to simply switch to the project I need to work on, do the work, and then switch to another project. Switching between projects is as easy as Cmd+Ctrl+P if you're working on a Mac or Ctrl+Alt+P if you're working in Windows or Linux. This brings up a list of projects that Sublime nows about and lets you switch projects without leaving the application or returning to the terminal.

The benefit of this is that I only have one window open for Sublime and I can stay focused on the code that I am writing for that particular project. Having multiple projects open is distracting to me and puts me off my work.

I'm not currently using the settings or build systems for a project, but I am looking into running tests from within Sublime and adding these to my project files as build systems.

Getting to know how your tools work and making them work better for you is the key to getting the most out of them. Investing a bit of time in organising your code with Sublime's project files make organsing and working with even multiple folder projects a breeze.

My Sublime Text 3 Setup

 •  Filed under Sublime Text, Programming, By Me

The text editor. The programmer's most important tool and the center of an ongoing debate that will occupy programmers for years.

I'm not going to debate on the best editor as that would be stupid. Everyone has their own preferences and needs. Vim does offer some nice advantages for those familiar with all the necessary keystrokes, but for my needs I just want a nice, extensible text editor that I am comfortable with. I chose Sublime Text 2 a couple of years ago and more recently I have started using Sublime Text 3 for my day to day coding needs.

The Essentials

Okay they're not really essentials, but if you can't even look at your text editor then you're already hitting your first hurdle. Not only does Sublime Text allow you to change the colour scheme and fonts for the different languages you code in, but it also allows you to change the theme of the editor. A nice touch I think you'll agree.

Theme

I settled on the Flatland theme a few months ago. Its non-gradient look might be a bit bare for some, but I do like this theme. It's very dark and I prefer the dark background in my sidebar and tabs.

It also allows you to customise the height of the folders and files in your sidebar. By default Sublime does have a fair margin around these, but Flatland allows you to specify a smaller size, so that you can fit more into your sidebar.

Colour Scheme

Long before I started using Sublime Text 2, I was already using the Solarized colour scheme. I've stuck with it ever since and even use it as the colour scheme for my terminal. I prefer the dark version as the light version does have too much of a stark contrast with the Flatland theme.

Font

I've tried lots of fonts for programming with, but I find that Monaco, a font shipped with OS X, is best suited to my needs. It looks nice on my MacBook Pro and on my external monitor with just a small difference between the two screens.

Installed Packages

I do have a fair number of packages installed for specific things like Cucumber, RSpec, Rails and other language and framework specific needs so I'm going to list those packages that are independent of language and therefore get used on a frequent basis.

AllAutoComplete

Sublime Text 2 just came with autocomplete listings for the file you had currently open, so this plugin is a welcome addition as it includes all currently open files for your autocomplete listing.

SublimeGit

This package is a great package with plenty of integration with Git. It's also the first paid package that I have used with Sublime Text 3. As I use Git all the time, this plugin been worth every penny.

GitGutter

GitGutter allows you to see which lines have been inserted, deleted and modified. It's basically a diff for your editor. Little symbols appear in your editors margin to signify where code has been changed. At a glance it's nice to see where I have modified a single file.

Origami

You can't use Vim and not be impressed with being able to split panes with just a few keystrokes. It was one of the few things I have managed to remember from my many sojourns to the land of Vim. Sublime Text does have the same ability to split panes, but the keystrokes for this are difficult to remember.

Origami makes splitting panes much easier by providing easier to remember keystrokes for manipulating and navigating through panes as well as having the same commands available in the command palette.

MarkdownEditing

I handle a lot of files in Markdown. It's the default markup for the majority of files that I create and use. Brett Terpstra's MarkdownEditing package provides some sensible defaults for editing Markdown files. It also provides some handy keyboard shortcuts for manipulating Markdown files such as inserting headings, pasting links and adding footnotes.

CTags

Although Sublime Text has built in support for finding symbols in the current file, having ctags support just makes navigating between methods a lot easier. Rather than switching to a file and searching for the symbol, I can simply use ctags to pull up all symbols for the project navigate straight to the method I need.

So there we go, a quick run down of my Sublime Text 3 setup. If you're looking for preferences files then I must do an update of these on Github, but I'll provide a link here when that's done in the next couple of days, so be sure to check back.