Search code examples
sublimetext3sublimetext-snippet

how to write a snippet than can toggle text like snippet Get Element


In snippet Get Element, when i type get and push tab, it will show

getElementsByTagName('')

and the letter T is highlight and editable, then typed letter I, it will change to

getElementById('')

automatically.

I want to create a snippet which can toggle text by the letter i typed, just like the snippet Get Element do, but i can't find where the snippet location. Anyone know its location or know how to create a snippet like that?


Solution

  • The snippet is inside the archived JavaScript package, which is located in the installation directory, and then Packages/JavaScript.sublime-package. Inside that package, the file's name is Snippets/Get-Elements.sublime-snippet and it has the following contents:

    <snippet>
        <content><![CDATA[getElement${1/(T)|.*/(?1:s)/}By${1:T}${1/(T)|(I)|.*/(?1:agName)(?2:d)/}('$2')]]></content>
        <tabTrigger>get</tabTrigger>
        <scope>source.js</scope>
        <description>Get Elements</description>
    </snippet>
    

    Basically, it works with conditional replace format strings. You can find the documentation for those in the boost regex docs, however I suggest to just have two different snippets in this situation with the triggers gett and geti respectively, since those still require the same number of key strokes but are way easier to create and maintain.


    You can open archived resource files like this easily with the PackageResourceViewer package.

    Details on what archived packages are: http://docs.sublimetext.info/en/latest/extensibility/packages.html