Search code examples
latexsublimetext3code-snippetssublimetext-snippet

Sublime Text consecutive snippets


Whenever I type a keyword and press tab to use my snippet, I have to place the cursor somewhere else with a mouse click to use another snippet. Thats not good at all. I want to be able to type tk;tab to get

\begin{tikzpicture}
  |
\end{tikzpicture}

Where my cursor is at the pipe. This works so far. But now I would like to for example type dr to be completed to \draw at this place. although I wrote this snippet, it doesn't work. So how can I use multiple snippets consecutively?

Very glad for every help


Solution

  • My guess would be that you're using $1 in your first snippet, so that it looks something like this (except I didn't use a latex scope):

    <snippet>
        <content><![CDATA[
    \begin{tikzpicture}
      $1
    \end{tikzpicture}
    ]]></content>
        <tabTrigger>tk</tabTrigger>
    </snippet>
    

    When Sublime expands a snippet, it lets you tab through all of the fields in numerical order, and then "exits" the snippet. The default exit point for a snippet is after the last character in the snippet.

    So if you were to expand this snippet, it would insert with the cursor at the position of $1, and now it's waiting for you to enter the content of field $1. If you look in the status line, you'll see it saying Field 1 of 2 to tell you that you're in the middle of a snippet (field 2 is where the snippet "ends").

    If your snippet works like that, you have to manually tell Sublime that you're done with the snippet, such as by moving the cursor or pressing Escape.

    The special snippet field $0 tells Sublime where you want the cursor to end up once you're done with the snippet. If you don't specify it explicitly, it's assumed to be at the end of the snippet text.

    If the whole of the snippet is just meant to expand to that text to let you enter what's inside, you can change the snippet to use $0 instead of $1.

    When you do that, after the initial text expansion Sublime finds no fields for you to enter and just exits the snippet immediately, leaving the cursor at position $0. From that point you can use a new snippet without any problems:

    Sample Multi Snippet Expansion