-
-
Notifications
You must be signed in to change notification settings - Fork 1
Snippets Part 3
Continuing on our snippet saga, let’s take a look at some more advanced things you can do inside snippet text beyond plain text.
Tabstops are positions in the text to place the cursor after the snippet is inserted and whenever the user hits the TAB key. A tabstop can contain default text that can either be accepted by hitting the TAB key or it can be replaced with user text simply by typing the replacement text.
Tabstops are created by inserting a DOLLAR SIGN followed by a number. The number used indicates the order that the TABS will be traversed. A value of zero indicates the final tabstop of the snippet. Entering the TAB key when the cursor is on the $0 position will simply insert a TAB character in the editing buffer.
To provide a default value to place at the given tabstop (otherwise known as a placeholder), use the following syntax:
${number:text}
Where number is the tabstop number and text is the default text to insert at the tabstop position. Keep in mind that the default text can itself contain additional snippet syntax.
If the same tabstop number is used in multiple places in the text (called a mirror in TextMate terms), TKE will position the cursor at the first occurrence and any text that is input at this point will also be input at the other tabstops with the same number. For example, the snippet:
for {int ${1:i}=0; $1<10; $1++} {
$0
}
Will insert a for loop, placing the cursor at the first $1 position (with a default value of “i”). If the user changes the text (let’s say “foo” and enters the TAB key, the value “foo” will be inserted at all three places where $1 is located.
TKE supports several variables that are expanded at the time the snippet is inserted into the text. The following is a list of just a few (see the User Guide for a full list):
- $CLIPBOARD (inserts the contents of the current clipboard)
- $CURRENT_LINE (inserts the text given at the current line)
- $DIRECTORY (inserts the directory of the current file)
- $FILENAME (inserts the file name of the current file)
- $CURRENT_DATE (inserts the current date in the form of MM/DD/YYYY)
- $CURRENT_TIME (inserts the current time in the form of HH:MM AM/PM)
You can execute a shell command from within a script that is run when the snippet is inserted by enclosing the command in backticks (`). The following is an example of its use. This snippet will create a link using the contents of the clipboard as the URL to link to and then position the insertion cursor between tags.
<a href='`pbpaste`.html>$0</a>
In the next post, we’ll take a look at the last special syntax called transforms. Until then, take advantage of these powerful snippet features to help you get even more out of your snippet usage.