Sublime Text for Drupal - Key Bindings and Snippets

Sublime Text for Drupal - Key Bindings and Snippets

By Boris Baldinger 23rd July 2012 Drupal

This is the third and final part of my Sublime Text 2 (ST2) blog series. After giving an introduction and looking installation and configura- tion of ST2, I will talk about the key bindings and snippets.

Key Bindings

One of the biggest advantages of ST2 is that you have full control over most of its settings. It is really easy to configure ST2 to your personal needs.

Preferences

As you can see, there are two major parts of the settings. The first being the general configuration and the second one for the key bindings. This is the feature you should be really excited about. Imagine you can configure a text-editor exactly to your needs. ST2 let your dreams come true. To start, just open the "Key Bindings - Default" settings. There you can see all the predefined key bindings like:

{ "keys": ["super+c"], "command": "copy" },
{ "keys": ["super+k", "super+b"], "command": "toggle_side_bar" },

These are simple definitions of key bindings. As you can see it contains a key-definition and a command (here you can find a full list of available commands). You also can chain key strokes to start an action. But this isn't where the customization ends. There are more complex key bindings, where you can pass arguments and contexts like:

{ "keys": ["shift+enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
        [
                { "key": "setting.auto_indent", "operator": "equal", "operand": true },
                { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
                { "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
                { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
        ]
}

If you want to create new key bindings or overwrite existing ones just open the "Key Bindings - User" file and create your own bindings there. After saving the new bindings will be active.

For a detailed documentation of customizing your key bindings please visit the unofficial documentation of ST2. There is a part for the customization and a reference for the more complex configs.

The last item I have left to share on this matter is a free tool availabled for Mac users called Cheat Sheet (AppStore Link). Once installed, it shows you all available key bindings for your currently active application.

My most used keyboard commands

Keyboard Command Description
CMD + SHIFT + P Open the command palette
CMD + P
CMD + T
Goto anything...

CMD + R
CMD + P followed by @

Goto symbol... (in php goto function)
CTRL + G
CMD + P followed by :
Goto line...
ALT + . Reveal actual file in sidebar
CMD + SHIFT + F Find in files...
CMD + F Find in actual file...

Snippets (for Drupal)

Another great feature of ST2 are the snippets. Snippets are intelligent templates that will convert some short codes into a predefined text block with placeholders. 

To create a snippet, simply go to Tools -> New Snippet... you will get a some lines of code which you can adapt easily. Let's say you write PHP code and you need a lot of functions. You can create a snippet for exactly this purpose

<snippet>
  <content><![CDATA[
// ${1: description}
// ${2: variable}
function ${3:functionname}() {
  // ${4:some comment}
}
]]></content>
  <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
  <tabTrigger>fct</tabTrigger>

  <!-- Optional: Set a scope to limit where the snippet will trigger -->
  <scope>source.php</scope>
</snippet>
In this snippet you have to write "fct" and hit the "tab" key on your keyboard. ST2 will fill in the content-part of your snippet. While you are "tabbing through" all the defined placeholders, ST2 will automatically create the selection, so that you can overwrite them.
And also this snippet is only available if the source syntax is PHP. Snippets can be saved under any package folder. The extension for a ST2 snippet will be ".sublime-snippet".

Full credit to Márton Juhász who has done some great work for the Drupal community. He created a package for ST2 which contains a lot of nice snippets for the use with Drupal.
You can install it by pressing "cmd+shift+p" type "install" and search for "Drupal Snippets". Hit the "return" key and you're all set up.

But there is one problem... you don't know all the tab triggers of these Drupal snippets yet. I can help you with that: Just hit "cmd+shif+p" and type "sn hoo" or "sn h_" and you will get a list with all available tab triggers for the Drupal snippets. It's that easy.

There is a more in depth documentation of snippets available.

Conclusion

As you can see, there is a lot of community love around ST2. And if you take your time, you will love all the features and configuration possibilities. I am still getting used to the editor, but my addiction to this piece of software is growing from day to day.

Comments

bandar bola's picture
bandar bola

It's nearly impossible to find knowledgeable people on this subject, but you seem like you know what you're talking about! Thanks
bandar bola

Add comment

Search form

Latest comments

  • home business software programs's picture

    Good day! I simply wish to give a huge thumbs up for the nice info
    you have got here on this post. I will probably be coming again to your weblog for more soon.

    home business s...
  • Agentur Typo3 Programmierung at Steilstarter's picture

    Hello,

    I must say that you are going great. you are sharing your information with us so thanks for that.

    Agentur Typo3 P...
  • Daniel's picture

    By the way, by "no documentation" I´m referring to Views Autorefresh. It is either not complete or conflicting.

    Daniel