Smart Drupal snippets in Sublime

Published by Lennart Van Vaerenbergh on May 5, 2015

There are several plugins out there containing Drupal snippets. But most of the time it's incomplete and missing stuff, or it's just not how I like it. Luckily, it's easy to add your own snippets for example for hooks, full blown with prefilled module names. For your information, I'm using Sublime Text 2, but it can't be that different in version 3.

Let's have a look how we can add snippets for Drupal hooks.

In Sublime, go to Tools > New Snippet... . An example snippet opens up like this:
<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>
Your snippet code comes in between the CDATA brackets. In the example, this is where the 'Hello' starts, right up until the period.
After the content closing tag, we see some metadata of the snippet.
tabTrigger: This is the word on which the snippet is triggered while typing.
scope: This is the file type for which the snippet may appear. In our case, this will be 'source.php'

Let's make a snippet for hook_menu. Copy the following code into the snippet:
<snippet>
  <content><![CDATA[
/**
 * Implements hook_menu().
 */
function ${TM_FILENAME/(.+)\..+|.*/$1/:name}_menu() {
  \$items = array();

  \$items['example_item'] = array(
    'title' => '',
    'description' => '',
    'page callback' => '',
    'page arguments' => array(),
    'access arguments' => array(),
    'type' => MENU_NORMAL_ITEM,
  );

  return \$items;
}
]]></content>
  <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
  <tabTrigger>hook_menu</tabTrigger>
  <!-- Optional: Set a scope to limit where the snippet will trigger -->
  <scope>source.php</scope>
</snippet>
Important to know is that every php variable starting with $ must be escaped using a backslash \.
There are a couple of variables available in the snippets. More information about all variables can be found here. To print the module name (which is basically the filename minus the extension), we need to tamper the filename a bit so the extension is cut off: ${TM_FILENAME/(.+)\..+|.*/$1/:name}.

Our snippet is ready for saving on the right spot. Click File > Save As... and save the file in Packages/User/Snippets/hook_menu.sublime-snippet. It's important the file has the extension .sublime-snippet in order to get recognized. All Packages directories will be scanned for these files by Sublime.

The snippet is ready for use now. Sublime doesn't have to be restarted. Just go to a module file and start typing 'hook_menu' and hit enter or tab. Behold the magic. Have fun!

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
CAPTCHA
This challenge is for testing whether or not you are a human visitor and to prevent automated spam submissions.