We can use templates to improve our productivity when a piece of code or instruction appears repeatedly
As a lightweight code editor, VSCode is very popular in the industry. Here’s how to make code templates with VSCode Snippet.
Create a Snippets
- Select File on the menu bar
- Select Preferences from the drop-down menu
- Select the User Snippets, as shown below, and select the corresponding language. If the language you need is not available, you need to install the language plug-in.
The Snippets content is defined in JSON format.
For example, IF I want to create a new Vue template, select Vue. Json (Vue).
Then enter the following JSON code:
{
"vue template": {
"prefix": "vuem",
"body": [
"<template>",
" <div>",
" ",
" </div>",
"</template>",
"<script>",
" export default {",
" name:'$1',",
" data () {",
" return {",
" ",
" }",
" },",
" computed:{",
" ",
" },",
" methods:{",
" ",
" },",
" components: {",
" ",
" },",
" }",
"</script>",
"<style scoped>",
" ",
"</style>",
""
],
"description": "vue template"
},
}
Copy the code
Save, and then create a.vue file and typevuem+tab
或 vuem+enter
Generating a template
vue template
: The current snippet name.prefix
: prefix, code block using shortcut; Type the prefix, press TAB, and the code block will be used.body
: Code block content; Newline use \r\n.description
: type the prefix, vscode senses the prefix, and the description is displayed.At $1, $2, $0
: Specifies the position where the edit cursor appears after the code module is generated. Use the Tab key to switch (edit cursor pressAt $1, $2, $3... $0
Sequence jump),$0
Is the last position to switch the cursor.
The Snippet grammar
Tabstops
$1, $2 specifies the cursor position after the code block is generated; The cursor appears at the same $1 position at different locations.
Placeholders
Add a default value to the cursor position. For example, ${1:another ${2:placeholder}}; The default value at $1 is another.
Choice
Cursor position setting multiple values to choose from; For example, ${1 | one, two, three |}; $1 can be filled with one,two, or three.
Variables
- Commonly used variables
TM_SELECTED_TEXT
Currently selected or an empty stringTM_CURRENT_LINE
Current line contentsTM_CURRENT_WORD
Cursor character or empty stringTM_LINE_INDEX
The line number starting from 0TM_LINE_NUMBER
The line number starting from 1TM_FILENAME
Name of the document being editedTM_FILENAME_BASE
The name of the current edited document without a suffixTM_DIRECTORY
Directory of the current edited documentTM_FILEPATH
Full path of the current edited documentCLIPBOARD
Current clipboard contents
- Date and time related variables
CURRENT_YEAR
The current yearCURRENT_YEAR_SHORT
Both current and New YearCURRENT_MONTH
Month is a two-digit number, for example, 02CURRENT_MONTH_NAME
Full name of month, e.g. ‘July’CURRENT_MONTH_NAME_SHORT
Short for month, e.g. ‘JulCURRENT_DATE
One dayCURRENT_DAY_NAME
The day of the week, for example ‘Monday’CURRENT_DAY_NAME_SHORT
Short for the day of the week, ‘Mon’CURRENT_HOUR
Hourly, 24-hour systemCURRENT_MINUTE
minutesCURRENT_SECOND
Number of seconds
- Variable formatting
${TM_FILENAME/(.*)\\.. +$/$1/} | | | | | | | |-> no options | | | | | |-> references the contents of the first | | capture group | | | |-> regex to capture everything before | the final `.suffix` | |-> resolves to the filenameCopy the code
A python snippet
"python template": { "prefix": "pyHeader", "body": [ "#!user/bin/python" "# _*_ coding: utf-8 _*_" " " "# @File : $TM_FILENAME" "# @version: 1.0" "# @author: XXXXXXX ""# @email: XXXXXXX" "# @time: $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND" "#Description:" " " "import datetime" "start_time = datetime.datetime.now()" "end_time = datetime.datetime.now()" "print(end_time-start_time)" ], "description": "my vue python template", }Copy the code
In fact, the following syntax is just a casual look, there is nothing else to say, ok, that’s it