The Clipboard Variable
It is a common joke that programming is little more than copy-pasting from stack overflow; regardless of the programmer's experience, one will find that they are copy-pasting from API documentation, etc. This can be combined with VS Code’s $CLIPBOARD
variable to allow you to format your copied text with snippets. A simple example would be to paste it with double quotes.
"String paste": {
"prefix": "str",
"body": [
"\"${1:$CLIPBOARD}\"$0"
],
"description": "allows copied text to be surrounded using double quotes"
}
Comment Variables
If you use snippets to create comments, you will find it annoying to have to create a snippet for every language that you use. Fortunately, there are variables that will insert the correct comment syntax for the current language. This allows you to simply create the snippet globally and use it for all your languages.
For an in-line comment, you can use the $LINE_COMMENT
variable; a common use case would be a TODO comment.
"todo": {
"prefix": "todo",
"body": ["$LINE_COMMENT TODO ${1:remove}", "$0"],
"description": "add a todo comment"
}
Also, there are variables for block comments. $BLOCK_COMMENT_START
opens the block comment, and all the following lines will be formatted correctly to be in the block comment, for example, by prefixing them with asterisks in PHP. The block comment can then be closed with the $BLOCK_COMMENT_END
variable.
"Insert Doc block": {
"prefix": "doc_block",
"body": [
"$BLOCK_COMMENT_START",
"@author Pieter van der Meijden",
"$1",
"$BLOCK_COMMENT_END"
]
},
Date Time Variables
Snippet variables can also be used to output the current date and time in the format that you would like. First, you can get the full year with $CURRENT_YEAR
or just the last two digits with $CURRENT_YEAR_SHORT
. For the month, you can get it as a number with $CURRENT_MONTH
, the name with $CURRENT_MONTH_NAME
, or the shortened name with $CURRENT_MONTH_NAME_SHORT
. The day of the month is simply the $CURRENT_DATE
variable, while the day of the week has a full name with $CURRENT_DAY_NAME
and a short variant with $CURRENT_DAY_NAME_SHORT
. For the time, it can be outputted in 24-hour format using the $CURRENT_HOUR
, $CURRENT_MINUTE
, and $CURRENT_SECOND
variables. If needed, there is also Unix time $CURRENT_SECONDS_UNIX
. An use case for the date time variables would be adding it to a doc block.
"Insert Doc block": {
"prefix": "doc_block",
"body": [
"$BLOCK_COMMENT_START",
"@author Pieter van der Meijden",
"@date $CURRENT_DATE-$CURRENT_MONTH_NAME_SHORT-$CURRENT_YEAR $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
"$1",
"$BLOCK_COMMENT_END"
]
}
File Variables
Sometimes you want to use the file name that you are working with for doc blocks, and VS Code allows you to extract this and more in your snippets with the following variables: You can use $TM_FILENAME
for the file name with the extension and $TM_FILENAME_BASE
for the name without. If you want the path for the current file, use $TM_DIRECTORY
, or if you want the file name with the extension appended, use $TM_FILEPATH
. Additionally, you can get the current VS Code workspace using $WORKSPACE_NAME
.
Line Variables
Another set of useful variables for extracting information about the line that you are currently on you can get the number of the line for either a zero or nonzero index with the $TM_LINE_INDEX
and $TM_LINE_NUMBER
variables, respectively. Also, you can get the contents of the line using the $TM_CURRENT_LINE
variable, or for simpler snippets, you can just get the word that is underneath the cursor using the $TM_CURRENT_WORD
variable.