Question

I am creating some snippets in ST3 that are essentially divs with special classes. After using the tab trigger to initiate the snippet the cursor is always at the end of the snippet, can I specify where the cursor is after the snippet is initiated?

Was it helpful?

Solution

Use $0 to indicate the exit point, and $1, $2, etc. to indicate different tab stops within the snippet, and the form ${1:foo} to indicate a default value. This way you can customize your code when you run the snippet, for example being able to input different class names or ids. For example, the following code creates a new div when you type newdiv, allowing you to customize the id and class by hitting Tab to move from field to field. When those are completed, the cursor ends up between the opening and closing tag:

<snippet>
    <content><![CDATA[
<div id="${1:foo}" class="${2:bar}">$0</div>
]]></content>
    <tabTrigger>newdiv</tabTrigger>
    <scope>text.html</scope>
</snippet>

Please see the snippets page at the unofficial docs, as well as the snippets reference for more information, including how to work with selections and using regexes on content or input. There are also certain environment variables available regarding the current document and working environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top