4

Find myself frequently invoking a snippet within a snippet, but of course when I go to expend the nested snippet, the tab key moves me to either the next entry of the first snippet or the end of the first snippet (at which point, I have to replace the cursor at the end of the tab-trigger expression for the 2nd snippet and hit tab, at which point the 2nd snippet is expanded).

eg. given snippet [ content A ${1:First point in A} and ${2: Second point in A} ] with tab-trigger tabtrigA and snippet [ content B ] with tab-trigger tabtrigB

I'd like to be able to do the following:

In[1]:

tabtrigA % Hit tab to expand snippet A

Out[1]:

 [ content **A First point in A** and ${2: Second point in A} ] % where everything between ** ** is highlighted

Now replace **...** content with tabtrigB

In[2]:

     [ content tabtrigB* and ${2: Second point in A} ] % where * marks location of cursor.

and hitting tab would result in:

Out[2]:

 [ content [ content B ]* and ${2: Second point in A} ] % where * marks location of cursor

and hitting tab again would then jump to second entry of snippet A

Obviously this is tiresome: is it possible to switch the priority assignment of tab so that it first acts as a tab-trigger and only jumps to next entry if there is no tab-trigger?


Update: as of April 2019, still no solution for triggering a snippet within a snippet.

2
  • have you tried setting "auto_complete_with_fields": true, in your user preferences file? Commented Apr 23, 2017 at 18:42
  • @KeithHall - yes, this doesn't seem to change the behavior: it still jumps to the next entry in the snippet (or to the end of the snippet) instead of triggering the 2nd snippet. Please let me know if you have any other ideas - been searching for a while on this one and it is really tiresome! Commented May 9, 2017 at 14:20

1 Answer 1

3

I don't think sublime can tell this snippets' next_field from that snippet's next_field. You can only ask if it has_next_field, any. But you could go with workarounds:


Trigger your nested snippet with something else than TAB:

  1. Through command palette by giving your nested snippet a description. The snippet below will be callable from the palette as Snippet: description_for_command_palette.


<snippet>
<content><![CDATA[
[ content B ]
]]></content>
<description>description_for_command_palette</description>
</snippet>
  1. Through keybinding to the snippet path:

{ "keys": ["ctrl+0"], "command": "insert_snippet", "args": {"name": "Packages/User/your_snippet.sublime-snippet"}}

  1. Through a keybinding to an anonymous snippet:

{ "keys": ["ctrl+0"], "command": "insert_snippet", "args": {"contents": "[ content B ]"}}


Overwrite the keybinding to go to the next field in snippet to, say, enter.

Just add two keybindings:

{ "keys": ["enter"], "command": "next_field", "context":
[
{ "key": "has_next_field", "operator": "equal", "operand": true }
]
},
{ "keys": ["tab"], "command": "insert_best_completion", "args": {"default": "\t", "exact": false}, "context":
[
{ "key": "has_next_field", "operator": "equal", "operand": true }
]
}
Sign up to request clarification or add additional context in comments.

1 Comment

The answer to this question provides another solution in the case that there is only one field in the snippet. In short, it obtains the desired result by specifying exit-placement of the cursor using ${0:}. Of course this wouldn't work for the case described here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.