Path: blob/main/src/vs/editor/contrib/snippet/browser/snippet.md
3296 views
Tabstops
With tabstops you can make the editor cursor move inside a snippet. Use $1
, $2
to specify cursor locations. The number is the order in which tabstops will be visited, whereas $0
denotes the final cursor position. Multiple tabstops are linked and updated in sync.
Placeholders
Placeholders are tabstops with values, like ${1:foo}
. The placeholder text will be inserted and selected such that it can be easily changed. Placeholders can nested, like ${1:another ${2:placeholder}}
.
Choice
Placeholders can have choices as values. The syntax is a comma-separated enumeration of values, enclosed with the pipe-character, e.g. ${1|one,two,three|}
. When inserted and selected choices will prompt the user to pick one of the values.
Variables
With $name
or ${name:default}
you can insert the value of a variable. When a variable isn't set its default or the empty string is inserted. When a variable is unknown (that is, its name isn't defined) the name of the variable is inserted and it is transformed into a placeholder. The following variables can be used:
TM_SELECTED_TEXT
The currently selected text or the empty stringTM_CURRENT_LINE
The contents of the current lineTM_CURRENT_WORD
The contents of the word under cursor or the empty stringTM_LINE_INDEX
The zero-index based line numberTM_LINE_NUMBER
The one-index based line numberTM_FILENAME
The filename of the current documentTM_FILENAME_BASE
The filename of the current document without its extensionsTM_DIRECTORY
The directory of the current documentTM_FILEPATH
The full file path of the current documentRELATIVE_FILEPATH
The relative (to the opened workspace or folder) file path of the current documentCLIPBOARD
The contents of your clipboardWORKSPACE_NAME
The name of the opened workspace or folderWORKSPACE_FOLDER
The path of the opened workspace or folder
For inserting the current date and time:
CURRENT_YEAR
The current yearCURRENT_YEAR_SHORT
The current year's last two digitsCURRENT_MONTH
The month as two digits (example '02')CURRENT_MONTH_NAME
The full name of the month (example 'July')CURRENT_MONTH_NAME_SHORT
The short name of the month (example 'Jul')CURRENT_DATE
The day of the monthCURRENT_DAY_NAME
The name of day (example 'Monday')CURRENT_DAY_NAME_SHORT
The short name of the day (example 'Mon')CURRENT_HOUR
The current hour in 24-hour clock formatCURRENT_MINUTE
The current minuteCURRENT_SECOND
The current secondCURRENT_SECONDS_UNIX
The number of seconds since the Unix epoch
For inserting random values:
RANDOM
6 random Base-10 digitsRANDOM_HEX
6 random Base-16 digitsUUID
A Version 4 UUID
Variable-Transform
Transformations allow to modify the value of a variable before it is being inserted. The definition of a transformation consists of three parts:
A regular expression that is matched against the value of a variable, or the empty string when the variable cannot be resolved.
A "format string" that allows to reference matching groups from the regular expression. The format string allows for conditional inserts and simple modifications.
Options that are passed to the regular expression
The following sample inserts the name of the current file without its ending, so from foo.txt
it makes foo
.
Placeholder-Transform
Like a Variable-Transform, a transformation of a placeholder allows changing the inserted text for the placeholder when moving to the next tab stop. The inserted text is matched with the regular expression and the match or matches - depending on the options - are replaced with the specified replacement format text. Every occurrence of a placeholder can define its own transformation independently using the value of the first placeholder. The format for Placeholder-Transforms is the same as for Variable-Transforms.
The following sample removes an underscore at the beginning of the text. _transform
becomes transform
.
Grammar
Below is the EBNF for snippets.
Escaping is done with with the \
(backslash) character. The rule of thumb is that you can escape characters that otherwise would have a syntactic meaning, e.g within text you can escape $
, }
and \
, within choice elements you can escape |
, ,
and \
, and within transform elements you can escape /
and \
. Also note that in JSON you need to escape \
as \\
.