Sublime Forum

Snippet arguments

#1

Hi,

I would like to give some arguments to a snippet. But I don’t know how to do this ?

my snippet :

<snippet> <content><![CDATA[ Hello $PARAM1 in $TM_FILENAME ]]></content> </snippet>
So how can I give an angument to a snippet ?

in my plugin:

self.view.run_command("insert_snippet", {"name": "Packages/Bootstrap/test.sublime-snippet"})

Thanks.

0 Likes

#2

Did you find the solution to this…its driving me mad!

0 Likes

#3

view.run_command(‘insert_snippet’, dict(contents="${PARAM1}s", PARAM1=“it works!!”))

0 Likes

#4

Thanks Castles (Do you mind if I call you ‘Castles’? :wink:

Its not the same as ‘including’ a snippet though, which is far more useful.

self.view.run_command(“insert_snippet”, {“name”: “Packages/Test/snippet_with_param.sublime-snippet”, PARAM1=“it doesnt work!!” })

0 Likes

#5

I found this page because I was searching for this same answer. I’m creating a package that I want my users to be able create their own (possibly massive) Snippet files with their own dynamic ${PARAMn} placeholders to be filled by a single command. I finally figured out how to do it, and from what I can see, is the first reply that actually answers the question.

This Snippet in Packages/User/test_with_args.sublime-snippet:

<snippet>
    <content><![CDATA[
Hello, param1 = [${PARAM1}] param2 = [${PARAM2}] and param3 = [${PARAM3}].
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- <tabTrigger>hello</tabTrigger> -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source comment</scope>
</snippet>

with this in your command:

args = {
    'name': 'Packages/User/test_with_args.sublime-snippet',
    'PARAM1': "alpha",
    'PARAM2': "bravo",
    'PARAM3': "charlie"
}
self.view.run_command("insert_snippet", args)

Produces this output:

Hello, param1 = [alpha] param2 = [bravo] and param3 = [charlie].
0 Likes

#6
1 Like

#7

Wow! Very cool! I will try it.

This stuff needs to get into the documentation… Is there any way the official Sublime team would let me overhaul their documentation to bring it up to date and be thorough? I have a resume that would support it. I’m a writer + developer combined, and the very best time for me to write stuff is while I am learning it so I can see what’s missing from the documentation and fill it in… Just from past experience.

0 Likes

#8

Actually, assuming I provide the calc_date() function accessible from within the command:

self.view.run_command("insert_snippet", {"contents": "${1:DATE} $2", "DATE": calc_date()})

does not work on my system. It only produces the string “DATE”. But this syntax:

self.view.run_command("insert_snippet", {"contents": "The date: ${DATE}", "DATE": calc_date()})

my calc_date() function returns the string “17-May-2025 13:59” and inserting the snippet produces this string: “The date: 17-May-2025 13:59”.

Very pleasing! That’s even MORE flexible! I could provide a number of commonly-used computed values (over and above the list of “environment variables” documented in the unofficial documentation), like the filename stem (w/o extension), and other things used in the context I am working with.

Thank you, Keith!

0 Likes

#9

Numbered arguments work as follows:

view.run_command("insert_snippet", {"contents": "${1:DATE} $2", "1": "foo", "2": "bar"})

expands to foo bar.

1 Like

#10

Ahhhh… I see the pattern! Thank you! It appears this applies to any string (number strings included). Indeed…

self.view.run_command("insert_snippet", {"contents": "${1} $2 ${ALPHA}", "1": "foo", "2": "bar", "ALPHA": "baz"})

expands to foo bar baz.

0 Likes