Sublime Forum

How do I setup "--" as a snippet tab trigger?

#1

I am currently working with fortran and I’m adding some snippets to make the workflow faster. Since fortran does not support the ++ or – operators, I would like to make snippets that will change these operators to the explicit increment and decrement statements. For example

a++

becomes

a = a + 1

and

a--

becomes

a = a - 1

I have successfully implemented the ++ snippet as follows.

<snippet>
    <content><![CDATA[ = $TM_CURRENT_WORD + 1]]></content>
    <tabTrigger>++</tabTrigger>
    <description>i = i + 1</description>
    <scope>source.fortran</scope>
</snippet>

However, my implementation of the – snippet fails to trigger.

<snippet>
    <content><![CDATA[ = $TM_CURRENT_WORD - 1]]></content>
    <tabTrigger>--</tabTrigger>
    <description>i = i - 1</description>
    <scope>source.fortran</scope>
</snippet>

Does anyone have any suggestions on how to get the – snippet to trigger? Thanks.

0 Likes

#2

Weird. Tab trigger of ++ or – works fine for my snippets on ST3.

0 Likes

#3

@qgates,

Does the – snippet trigger for you when it is appended to other text? For example – will trigger for me, but a-- will not trigger.

0 Likes

#4

Ahh, and therein lies your problem. Snippets are designed to be triggered on whole words (so far as sublime sees it). The fact that a++ works for you is more by luck than by design, because for some reason sublime sees ++ as a separate “word”. But it won’t for almost anything else, except in a few other edge cases.

This is actually a good thing, because otherwise you’d get into a mess with snippets that have some characters of a word being triggered accidentally. For example, in your methodology, if you defined a tab trigger “bc”, then typed “abc” the “bc” snippet would be triggered. The vast majority of the time we’d want to avoid that.

Given what you want to expand your snippet to, wouldn’t it be better to trigger ++ and – to something like:

<snippet>
	<content><![CDATA[
${1:var} = $1 + 1
]]></content>
	<tabTrigger>++</tabTrigger>
	<scope>source.fortran</scope>
</snippet>

That way, you just type ++ (or --, with the above appropriately modified), on its own, type your variable name once and it gets filled out twice, then hit to go to the end, or hit Ctrl+enter to go straight to a newline. That would seem the most logical to me. You could also make your constant a placeholder too, so you could change your increment amounts. Use this in your snippet:

${1:var} = $1 + ${2:1}

By the way, with these approaches, Using or <Ctrl+Enter> will leave you in field-editing mode, because snippets can be multiline. Press ESC to clear field editing.

0 Likes