Sublime Forum

Snippets for newbie

#1

Hi all,

Sorry if this is answered elsewhere, but I haven’t found it if it is!

I want to be able to insert a snippet into my code with a parameter in the text, so:

///////// Mod Vx Start
//

//
///////// Mod Vx End

where x is the version number this mod is applied to. I’m guessing that I could make it an environment variable like, say, $MODNO “12.2.3”. So my snippet would then be:

///////// Mod V$MODNO Start
//

//
///////// Mod V$MODNO End

This would mean I would only have to define $MODNO once for each version I was working on. What I can’t figure out is how to set up the environment variable, which would be ideally done through some user input. The documentation says that custom variables are held in a file .sublime-options, but I can’t find one anywhere to use as a template, nor do I know the format for defining an environment variable. Is what I want to do possible, or am I really just talking a load of rubbish?

Or do I have to code up a package to achieve what I want?

Thanks in advance for any comments!

Ali

0 Likes

#2

Curious where you read about that, because I have never heard of such file extension.

What you want to do is define shellVariables using .tmPreferences files: docs.sublimetext.info/en/latest/ … f-settings

You can then use the defined shellVariables as “$VARIABLE_NAME” (upper case, by convention) in snippets. The documentation is a bit incomplete in that regard.

0 Likes

#3

Hi FichteFoll,

It was in the ST3 Unofficial Documentation section 2.9.3 - Snippet Features.

Thanks for the help, I’ll try that out tomorrow (too late now!)

Ali

0 Likes

#4

Thanks FichteFoll,

Got that working now! Lots of fiddling around, but got there in the end. Thanks for your help.

Regards

Ali

0 Likes

#5

Hi, all!
Is there a way that one of those variables that we can define in .tmPreferences files, as a shellVariables elements, get the current date from some .py script/plugin? Or any way, for that matter, to set the current date to one of those vars?

0 Likes

#6

Yes, a plugin could automatically generate .tmPreferences files with the current time/date (and other information), but you could also use macros like this one. I’m also unsure how frequently tmPreferences files are read by ST.

0 Likes

#7

Thanks Fichte! I’ve seen that macro before, and i set it aside because i doubted that a macro could be “played” the same way a snippet can. Can a macro be played in the same way? I mean, by the “tabTrigger”, as the snippet?
Say, for example, that my snippet gets inserted when i type “comment” + TAB. Do i get to play a macro like that the same way or, say, i should press Ctrl+Shift+c, for instance?
It may be (i hope) that i got a little lost between macros, commands, snippets and completions. And it may be (i hope even more) that you can guide me in the right direction.
Thanks again!

0 Likes

#8

The macro cannot be run like normal snippets using auto completion, but you can “hack” something together using a clever key binding.

A key binding like follows would execute the macro from “User/File Name of Our.sublime-macro” when the Tab key is pressed after the text “now”:

  { "keys": ["tab"], "command": "run_macro_file",
    "args": {"file": "Packages/User/File Name of Our.sublime-macro"},
    "context": [
      { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
      { "key": "preceding_text", "operator": "regex_contains", "operand": "now$", "match_all": true },
    ]
  }
0 Likes

#9

Many thanks for your help, FichteFoll.
I’m about to get where i wanted. I was trying to set a snippet (a macro now, since your intervention), that allows me to add a comment in php code with the opening comment mark (/), my name, date | time, the closing comment mark (/) and the cursor in between, so i can write my comment.
I’ll look closer into macros, now, because i need to tell apart if i’m inside a <?php ?> section or outside of it (in a html section), so to be able to add <?php ?> tags to the previously described content of the macro.
I’ve had accomplished this with a snippet, but not the “date | time” part. So, probably (thinking out loud), the scope for macros be similar to the scope in snippets, and i can make two different macros, one for each scope (inside <?php ?> or not), as i’ve done two snippets.
I’ll look little further into it and then post back the solution. It could be useful to others, i believe, since i’ve found many questions regarding this, but no answer where all is put together.
Again, many thanks to you.

0 Likes

#10

Ok! Got it! This is thanks to FichteFoll, of course. I’m just collecting it all and putting it here together.
Now, when i type “com” + [tab], it writes /* [My_Name] [date] | [time] ^ */, or <?php /* [My_Name] [date] | [time] ^ */ ?>, so i can enter comments quickly.

Here’s how i did it.
There are two macros and two key bindings.
First macro i called it comments_in.sublime-macro

[
    { "command": "move", "args": {"by": "words", "forward": false, "extend":true}},
    { "command": "insert_snippet", "args": {"contents": "/* Esteban: $1 | $2  $0 */"} },
    { "command": "insert_date", "args": {"format": "%x"} },
    { "command": "next_field" },
    { "command": "insert_date", "args": {"format": "%X"} },
    { "command": "next_field" }
]

Second macro, called comment_out.sublime-macro

[
{ “command”: “move”, “args”: {“by”: “words”, “forward”: false, “extend”:true}},
{ “command”: “insert_snippet”, “args”: {“contents”: “<?php/* Esteban: $1 | $2 $0 */ ?>”} },
{ “command”: “insert_date”, “args”: {“format”: “%x”} },
{ “command”: “next_field” },
{ “command”: “insert_date”, “args”: {“format”: “%X”} },
{ “command”: “next_field” }
]

Key BIndings:

{ “keys”: [“tab”], “command”: “run_macro_file”,
“args”: {“file”: “Packages/User/comment_in.sublime-macro”},
“context”: [
{ “key”: “preceding_text”, “operator”: “regex_contains”, “operand”: “com$”},
{ “key”: “selector”, “operator”: “equal”, “operand”: “source.php” }
]
},

{ “keys”: [“tab”], “command”: “run_macro_file”,
“args”: {“file”: “Packages/User/comment_out.sublime-macro”},
“context”: [
{ “key”: “preceding_text”, “operator”: “regex_contains”, “operand”: “com$”},
{ “key”: “selector”, “operator”: “equal”, “operand”: “text.html.basic” },
{ “key”: “selector”, “operator”: “not_equal”, “operand”: “source.php” }
]
}

Hope it helps someone!
Thanks FichteFoll!

2 Likes

#11

Yes, duplicating the snippets (with modification) and adding a context entry for each key binding is exactly the way. :+1:

0 Likes