vscode - 代碼模板
程序編寫時,一段代碼或說明會重復出現,使用模板有利於提高我們的工作效率。
VSCode 作為一種輕量級的代碼編輯器,業界內頗受歡迎;下面就介紹一下如何利用VSCode snippet 制作代碼模板。
創建一個snippets
- 菜單欄選擇File
- 下拉菜單中選擇 Preferences
- 再選擇User snippets,出現下圖情況,選擇對應的語言即可;如果沒有你需要的語言,你需要安裝對應的語言插件。
Snippets內容使用JSON格式進行定義。
一個JavaScript例子
{
"For_Loop": {
"prefix": "for",
"body": [
"for (const ${2:element} of ${1:array}) {",
"\t$0",
"}"
],
"description": "For Loop"
}
}
- For_Loop: 當前snippet名字。
- prefix: 前綴,代碼塊使用快捷方式;鍵入前綴,按tab鍵,代碼塊就會被使用。
- body: 代碼塊內容;換行使用\r\n。
- description: 鍵入前綴,vscode 感知到前綴,顯示的說明內容。
- $1,$2,$0: 指定代碼模塊生成后,編輯光標出現位置; 使用Tab鍵進行切換(編輯光標按$1,$2,$3...$0的順序跳轉),$0是光標最后可切換位置。
Snippet語法
Tabstops
$1
,$2
指定代碼塊生成后,光標出現的位置;不同位置的相同$1位置同時出現光標。
Placeholders
給光標出現位置加上默認值;例如,${1:another ${2:placeholder}}
;$1
處位置默認值是another
。
Choice
光標位置設置多個值可供選擇; 例如,${1|one,two,three|}
;$1
位置處可以選擇one
,two
,three
中一個詞填充在此處。
Variables
常用變量
TM_SELECTED_TEXT
當前選中內容或空字符串TM_CURRENT_LINE
當前行內容TM_CURRENT_WORD
光標處字符或空字符串TM_LINE_INDEX
從0開始的行號TM_LINE_NUMBER
從1開始的行號TM_FILENAME
當前被編輯文檔名TM_FILENAME_BASE
當前被編輯文檔名,沒有后綴TM_DIRECTORY
當前被編輯文檔目錄TM_FILEPATH
當前被編輯文檔全路徑CLIPBOARD
當前剪切板內容
日期和時間相關變量
CURRENT_YEAR
當前年CURRENT_YEAR_SHORT
當前年后兩位CURRENT_MONTH
月份,兩位數字表示,例如02CURRENT_MONTH_NAME
月份全稱,例如 'July'CURRENT_MONTH_NAME_SHORT
月份簡寫 ,例如'JulCURRENT_DATE
某天CURRENT_DAY_NAME
星期幾, 例如'Monday')CURRENT_DAY_NAME_SHORT
星期幾的簡寫, 'Mon'CURRENT_HOUR
小時,24小時制CURRENT_MINUTE
分鍾CURRENT_SECOND
秒數
變量格式化
${TM_FILENAME/(.*)\\..+$/$1/}
| | | |
| | | |-> no options
| | |
| | |-> references the contents of the first
| | capture group
| |
| |-> regex to capture everything before
| the final `.suffix`
|
|-> resolves to the filename
一個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",
}
config
// javascript.json
{
// Place your snippets for javascript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"Print to console": {
"prefix": "log|console",
"body": [
"console.log('$1')",
"$2"
],
"description": "Log output to console"
},
"For_Loop": {
"prefix": "for",
"body": [
"for (const ${2:element} of ${1:array}) {",
"\t$0",
"}"
],
"description": "For Loop"
},
"console.log": {
"prefix": "cl",
"body": [
"console.log($1)"
],
"description": "console.log"
},
"notes": {
"prefix": "/*",
"body": [
"/**",
" * @method $1",
" * @description $2",
" * @param $2",
" * @return $2",
" */",
],
"description": "notes 方法注釋"
},
"todo": {
"prefix": "todo|TODO",
"body": [
"// !!!TODO $1"
],
"description": "TODO: // !!!TODO $1"
},
}