[Drupal] How to set Geany editor snippets for Drupal

| | 3 min read

This article discuss about setting snippets for Geany, a light weight IDE especially for Drupal 7 web developers as per coding standards. For those whom doesn't know, snippets are a small piece of source code which are meant for reuse. For example: while coding instead of typing full if conditional statement structure for each time we can just reuse them by the means of creating snippet for the same.

Creation of snippet is simple as like coding in any language. Before implementing custom snippets for Drupal 7 as per coding satandard we have to study about geany snippets.conf file structure, which can be found in '/home/user/.config/geany'.
Firstly we can look on the keywords available

  1. '\n' OR '%newline%': Used for new line.
  2. '\t' OR '%ws%': Used for one indentation step.
  3. '\s': Used for white space.
  4. '%key%': Keys defined in special section.
  5. '%cursor%': Defines where the cursor has to be placed. We can use multiple cursors in one snippet and can be jumped to next cursor position by using 'Move snippet cursor' keybinding.

Now we can look on important sections available in snippets.conf file.

  • Default is where we define snippets for files type other than the defined or known file types.
  • Special is where we can define our own special configuration keywords like

    # special keys to be used in other snippets, cannot be used "standalone"
    # can be used by %key%, e.g. %brace_open%
    # nesting of special keys is not supported (e.g. brace_open=\n{\n%brace_close% won't work)
    # key "wordchars" is very special, it defines the word delimiting characters when looking for
    # a word to auto complete, leave commented to use the default wordchars

  • Keybindings is where we can define keybindings or keyboard shortcut for snippets defined.

    # Optional keybindings to insert snippets
    # Note: these can be overridden by Geany's configurable keybindings

  • PHP is where we define snippets for php files.

For Drupal 7 snippets, we have to define snippets in php section like:

if=// Documentation.\nif (%cursor%)%block_cursor%
els=else%brace_open%// Documentation.%brace_close_cursor%
eli=elseif (%cursor%)%brace_open%// Documentation.%brace_close_cursor%
whi=// Documentation.\nwhile (%cursor%)%block_cursor%
do=// Documentation.\ndo%block% while (%cursor%);%cursor%
swi=// Documentation.\nswitch (%cursor%)%brace_open%case %cursor%:\n\t\t%cursor%\n\t\tbreak;\n\tdefault:\n\t\t%cursor%\n}%cursor%
try=// Documentation.\ntry%block%\ncatch (%cursor%)%brace_open%// Documentation.%brace_close_cursor%
for=// Documentation.\nfor (%cursor%; %cursor%; %cursor%)%block_cursor%
fun=\n/**\n * Documentation.\n */\nfunction %cursor%(%cursor%)%block_cursor%
foe=// Documentation.\nforeach (%cursor% as %cursor% => %cursor%)%block_cursor%

You can try these by copy and paste on snippets.conf file's PHP section. Save the file and type if and press tab to see the geany snippets magic. The following will create if conditional statement structure and cursor will be focused on first '%cursur%', to move next step press the keyboard shortcut for 'Move snippet cursor' which can be set in edit -> preferences -> keybindings -> editor Action section. In these snippets definition you have noticed the special keywords such as '%brace_open%', '%brace_close%' and '%block%' which are defined in Special section as explained above.

Like this you can create or customize already available snippets. Firstly using snippets take time, gradually on practice these will reduce the time of development. Hope this will help you using geany editor for Drupal development. Have a good, stay connected for more such articles.