最近在熟悉用VSC做前端開發編輯器,發現snippets這個功能很好用,就是自動幫你輸入HTML標籤,安裝HTML Snippets外掛程式(0.1.0)後,直接在代碼裡面輸入html5然後敲斷行符號就會自動幫你把一個HTML5標準標籤模板給填充好,但是有一個小問題就是,自動填滿的效果是這樣的:
<!DOCTYPE html><html lang="en"> <head> <title></title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="css/style.css" rel="stylesheet"> </head> <body> </body></html>
這裡lang=”en”,我想修改預設填充成lang=”zh-CN”,小折騰了一下,找到了這個外掛程式的設定檔位於
C:\Users\Dexter\.vscode\extensions\abusaidm.html-snippets-0.1.0\snippets\snippets.json
開啟這個檔案,找到定義html5映射的部分,修改如下,重啟一下VSC,再輸入html5就變成想要的效果了
"html5": { "prefix": "html5", "body": [ "<!DOCTYPE html>", "<html lang=\"$1zh-CN\">", ~~這裡原來是en~~ "\t<head>", "\t\t<title>$2</title>", "\t\t<meta charset=\"UTF-8\">", "\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">", "\t\t<link href=\"$3css/style.css\" rel=\"stylesheet\">", "\t</head>", "\t<body>", "\t$4", "\t</body>", "</html>" ], "description": "HTML - Defines a template for a html5 document", "scope": "text.html" },
補充一下
也可以在檔案-喜好設定-使用者自訂程式碼片段,找到HTML後,在html.json設定檔裡面寫上自己常用的程式碼片段,例如下面這個是我修改的包含Vue.js引用的HTML5代碼模板:
{ /* // Place your snippets for HTML 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", "body": [ "console.log('$1');", "$2" ], "description": "Log output to console" }*/ "Vue": { "prefix": "vue", "body": [ "<!DOCTYPE html>", "<html lang=\"zh-CN\">", "\t<head>", "\t\t<title>$1</title>", "\t\t<meta charset=\"UTF-8\">", "\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">", "\t\t<script src=\"https://cdn.bootcss.com/vue/2.2.2/vue.min.js\"></script>", "\t</head>", "\t<body>", "\t$2", "\t</body>", "</html>" ], "description": "vue - Defines a template for a vue & html5 document" }}
這裡需要注意一下,代碼中$1,$2標註的位置是自動插入程式碼片段之後游標的位置,$1是預設位置,$2是按tab鍵之後會跳轉到的下一個位置