Snippets (Part 2)

In this article, we are going to look at TKE’s support for customized snippets that can be accessible from either any editing buffer (called “user snippets”) or from editing buffers of a specific language (called “language-specific snippets”). Because these snippets are custom made by the user, the snippet text can be anything you like and the trigger (the keyword that you input that is replaced with the snippet text) can also be any string that you like so long as each custom snippet has a unique trigger keyword associated with it.

First, let’s take a quick look at how to use a snippet.

Using a Custom Snippet – A Simple Example

Suppose you have created a custom snippet that displays the text “Hello World!” whenever you enter the keyword hw followed immediately by a trigger key (i.e, the SPACE, TAB or RETURN key).  Prior to the snippet expansion, your editing buffer might look like this:

Notice that the cursor is on the right side of the keyword hw. Now when we enter our trigger key (in this case we’ll use the TAB key), the text is magically transformed into the following:

Cool! The hw keyword was automatically removed from the editing buffer and the string “Hello World!” was put in place of it, saving ourselves several keystrokes in the process. Of course, there are only so many cases where using the string “Hello World!” aids in our day to day work, but I think you get the point.

So now that we have seen how snippets basically work, let’s take a look at how we create a custom snippet in TKE.

Creating a Custom Snippet

In the example above, I created a user snippet to expand the keyword hw to the string “Hello World!”.  User snippets can be used in any editing buffer, regardless of which language the editing buffer is currently set to. If I was creating a snippet that was only valid in HTML syntax, I could create a language-specific snippet that is only valid when I am editing code in the HTML language.

To create the snippet, select the Edit / Snippets / Edit User menu command.  This will display the file containing all of your user snippets in a new editing buffer.  Any saved changes that you make to this editing buffer will become immediately usable within TKE (therefore, no application restart is necessary).

Each snippet within this file should have the following syntax structure:

snippet keyword
	snippet text

Take note that there must be a TAB character preceding each line of your snippet and each snippet within the file must be separated by at least one line of whitespace. These whitespace requirements are necessary for TKE to properly discern what text in the file is snippet text verses other information in the file. If your snippet text contains whitespace lines, each whitespace line must be preceded by a TAB character as well.

So with this syntax structure in mind, our hello world snippet looks like the following:

Once we make this edit and save the file, you can now enter the keyword hw followed by the TAB key in any editing buffer from now on and have the string “Hello World!” be entered into the editing buffer. Nice!

In the next article, we’ll dig deeper into more exciting (and more useful) snippet syntax and examples.  As there is even more that you can do with snippet text than just static text replacement.

To see more information and download your copy of the TKE code editor, visit http://tke.sourceforge.net.

Advertisements
Snippets (Part 2)