Advertisement

Quick Tip: Banish Repetitive Tasks with Sublime Text 2 Macros

by

If there's one thing which can waste a lot of designers' and developers' time, it's repetitive coding. How often do you find yourself formatting and duplicating bits of code which don't really warrant making snippets from? Sublime Text 2 has just the tool to speed things up..


Watch the Tip

Alternatively, Download the video, or subscribe to Webdesigntuts+ screencasts via iTunes or YouTube!


Helpful Snippets

Here are my settings that I use at the moment. In my Default (OSX).sublime-keymap (which I open by going to Sublime Text preferences, and opening up “Key Bindings - User” ) I have the following:

[ 
{ "keys": ["super+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/New Line Semicolon.sublime-macro"} },
{ "keys": ["shift+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/New Line Array.sublime-macro"} },
{ "keys": ["alt+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/New Line Curly.sublime-macro"} },
{ "keys": ["super+;"], "command": "run_macro_file", "args": {"file": "Packages/User/EOL Semicolon.sublime-macro"} },
{ "keys": ["super+shift+."], "command": "run_macro_file", "args": {"file": "Packages/User/Array Arrow.sublime-macro"} },
{ "keys": ["super+alt+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/Double Semicolon.sublime-macro"} }
]

Just in case you’re wondering, this is a JSON Format. In “keys” you specify what to run, and in args.file - the location of the macro file to run.

Note: “super” is the name for mac users' “Command” button. Windows and Linux users should replace this with “ctrl” and use “Control” instead of “Command”. The file would look something like this:

[ 
{ "keys": ["ctrl+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/New Line Semicolon.sublime-macro"} },
{ "keys": ["shift+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/New Line Array.sublime-macro"} },
{ "keys": ["alt+enter"], "command": "run_macro_file", "args": {"file": "Packages/User/New Line Curly.sublime-macro"} },
{ "keys": ["ctrl+;"], "command": "run_macro_file", "args": {"file": "Packages/User/EOL Semicolon.sublime-macro"} },
{ "keys": ["ctrl+shift+."], "command": "run_macro_file", "args": {"file": "Packages/User/Array Arrow.sublime-macro"} },
{ "keys": ["ctrl+alt+shift+;"], "command": "run_macro_file", "args": {"file": "Packages/User/Double Semicolon.sublime-macro"} }
]

My Macros

If you find it helpful, here are a few of the macro files I use regularly.

Double Semicolon.sublime-macro
The “Double Semicolon” is what we created in the video (although it's pretty much useless) just so you can compare your results with mine:

[
  {
    "args":
    {
      "to": "eol"
    },
    "command": "move_to"
  },
  {
    "args":
    {
      "characters": ";"
    },
    "command": "insert"
  },
  {
    "args":
    {
      "characters": "\n\n"
    },
    "command": "insert"
  }
]

New Line Array.sublime-macro
Goes to the end of line, adds in a comma (“ , “) and advances to a new line.

[
    {
        "args":
        {
            "to": "eol"
        },
        "command": "move_to"
    },
    {
        "args":
        {
            "characters": ","
        },
        "command": "insert"
    },
    {
        "args":
        {
            "characters": "\n"
        },
        "command": "insert"
    }
]

New Line Curly.sublime-macro
A little helper to save one keystroke; automatically adds in a curly brace, and advances to a new line with alt+enter (remember, the keyboard shortcuts aren’t set in this file, but in the preferences file.)

[
  {
    "args":
    {
      "to": "eol"
    },
    "command": "move_to"
  },
  {
    "args":
    {
      "characters": " "
    },
    "command": "insert"
  },
  {
    "args":
    {
      "contents": "{$0}"
    },
    "command": "insert_snippet"
  },
  {
    "args":
    {
      "contents": "\n $0\n"
    },
    "command": "insert_snippet"
  }
]

EOL Semicolon.sublime-macro
Adds a semicolon at the end of the current line, no matter where the cursor is.

[
  {
    "args":
    {
      "to": "eol"
    },
    "command": "move_to"
  },
  {
    "args":
    {
      "characters": ";"
    },
    "command": "insert"
  }
]

New Line Semicolon.sublime-macro
Adds a semicolon at the end of the current line, and then advances to a new line.

[
  {
    "args":
    {
      "to": "eol"
    },
    "command": "move_to"
  },
  {
    "args":
    {
      "characters": ";\n"
    },
    "command": "insert"
  }
]

Array Arrow.sublime-macro
Best explained in the Quick Tip video - a nice helper for writing PHP Arrays.

[
  {
    "args":
    {
      "to": "eol"
    },
    "command": "move_to"
  },
  {
    "args":
    {
      "characters": " "
    },
    "command": "insert"
  },
  {
    "args":
    {
      "characters": "="
    },
    "command": "insert"
  },
  {
    "args":
    {
      "characters": ">"
    },
    "command": "insert"
  },
  {
    "args":
    {
      "characters": " "
    },
    "command": "insert"
  },
  {
    "args":
    {
      "contents": "'$0'"
    },
    "command": "insert_snippet"
  },
  {
    "args":
    {
      "to": "eol"
    },
    "command": "move_to"
  },
  {
    "args":
    {
      "characters": ","
    },
    "command": "insert"
  },
  {
    "args":
    {
      "by": "characters",
      "forward": false
    },
    "command": "move"
  },
  {
    "args":
    {
      "by": "characters",
      "forward": false
    },
    "command": "move"
  }
]
Advertisement