vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Support snippet variables

Open martinring opened this issue 10 years ago • 21 comments

The documentation on snippets says:

The snippet syntax follows the TextMate snippet syntax with the exception of 'regular expression replacements', 'interpolated shell code' and 'transformations', which are not supported.

Which implicitly says that VSCode does support variables.

The TextMate documentation on snippet variables says:

You can insert the value of a variable by prefixing the name of the variable with $. All the normal dynamic variables are supported, the most useful probably being TM_SELECTED_TEXT.

Though it does not work, nor does vscode seem to provide an alternative.


  • VSCode Version: 0.10.11
  • OS Version: Fedora 23

Steps to Reproduce:

  1. Create user snippet using variable TM_SELECTED_TEXT
  2. Use snippet -> Does not insert the selected text in place of $TM_SELECTED_TEXT

martinring avatar Apr 05 '16 10:04 martinring

Any chance this is going to be considered? :)

martinring avatar Apr 26 '16 15:04 martinring

@martinring I agree that our documentation does not explicitly mention that variables are not supported, but AFAIK they were never implemented. So this issue is a feature request.

alexdima avatar Apr 27 '16 13:04 alexdima

@alexandrudima ok thank you for the clarification. surrounding snippets are quite a common feature of editors. visual studio supports them as does textmate for example. I might look into adding that.

martinring avatar Apr 27 '16 13:04 martinring

I'll add in my vote. Being able to highlight text, run a snippet and have that highlighted text replace $TM_SELECTED_TEXT in the snippet is super useful. (Both Textmate and Sublime support this)

jreljac avatar Sep 30 '16 17:09 jreljac

desired variables: selected text, filename, date

jrieken avatar Nov 03 '16 15:11 jrieken

more resources http://manual.textmate.org/snippets.html#variables and https://manual.macromates.com/en/environment_variables.html

jrieken avatar Nov 03 '16 15:11 jrieken

We now support SELECTION, TM_SELECTED_TEXT, TM_CURRENT_LINE, TM_CURRENT_WORD, TM_LINE_INDEX, TM_LINE_NUMBER, TM_FILENAME, TM_DIRECTORY, TM_FILEPATH as defined here: http://docs.sublimetext.info/en/latest/extensibility/snippets.html#environment-variables.

jrieken avatar Nov 21 '16 15:11 jrieken

I would like to have a TM_FILENAME w/o the extension so i could, for exemple, create a snippet for auto naming a class base on the filename.

jbeaudoin11 avatar Jan 25 '17 21:01 jbeaudoin11

+1 for getting the filename without extension.

misantronic avatar Feb 05 '17 00:02 misantronic

Probably worth noting that (at least on the Sublime docs page) they do support this:

You can also use environment variables with substitutions:

# In file MyModule.js:
Transformation: ${TM_FILENAME/(\w+)\.js/\1/g}

# Output:
Transformation: MyModule

Seems like it might be unnecessary to add it as a full-fledged feature...

ianlyons avatar Feb 07 '17 17:02 ianlyons

Are we able to use regex ? If yes how ? Cause I'm not able to make it work..

With regex support yes it would be good enough. It would be nice if we could make custom variable like TM_FILENAME or simple shortcuts.

jbeaudoin11 avatar Feb 07 '17 19:02 jbeaudoin11

We are tracking the transformations request in #6920. It's a little more complex because of how the regex-engines in TextMate and VS Code/JS work. We are likely not shooting for compatibility in evaluating them, but just reuse the syntax.

jrieken avatar Feb 08 '17 08:02 jrieken

Any update on being able to retrieve the file name without the extension?

Ashedragon avatar Mar 10 '17 02:03 Ashedragon

Is Date as listed in https://github.com/Microsoft/vscode/issues/4956#issuecomment-258175578 still a thing to come?

ScallyGames avatar Mar 24 '17 09:03 ScallyGames

@Aides359 How isdate spec'd? I couldn't figure that out. Is it full UTC date string, locale data string, year, month, day and also time?

jrieken avatar Mar 24 '17 09:03 jrieken

@jrieken hm.. good point.

In lack of this feature I currently use vscode-insertdatestring (which is quite a hassle since I have to execute two commands per snippet) and they solve this issue by setting a property in the user settings with a sensible default.

Otherwise (not sure if that is possible syntax-wise) a solution might be to specify it using a parameter somewhat like ${date|'YYYY-MM-DD'} again with some sensible default.

ScallyGames avatar Mar 24 '17 11:03 ScallyGames

@Aides359 Yes, we need this. I'm currently TRYING to fully replace my workflow from WebStorm to VS Code for all the good reasons but always end up coming back to WS for heavy duty coding sessions. I use Live Templates(a term for snippets in WS) a lot with WS.

As an example, creating todo notes by expanding the todo keyword:

"body": [
    "/**",
    " * TODO($1): $2",
    " * @date - ${date|'YYYY-MM-DD'}",
    " * @time - ${date|'HH:mm'}",
    " */$0"
]

Should become:

/**
 * TODO(firstCursor): secondCursor
 * @date - 2017-05-19
 * @time - 00:29
 */

borislemke avatar May 18 '17 16:05 borislemke

@borislemke Is that a webstrom specific syntax, or where is that defined?

jrieken avatar May 19 '17 07:05 jrieken

@jrieken that is the proposal I would like VS Code to implement. The official WS syntax for the same template is:

/**
 * TODO($VAR1$): $VAR2$
 * @date - $DATE$
 * @time - $TIME$
 */$END$

borislemke avatar May 19 '17 08:05 borislemke

+1 for date, unless the format is not configurable cause i'd just rage over anything not YYYY-MM-DD.

$DATE(format)$ could be interesting.

$DATE(YYYY-MM-DD)$

bobmagicii avatar Jun 14 '17 17:06 bobmagicii