今回は、Visual Studio Code
でのスニペット登録を簡単に紹介します🧑🔧
ちなみにバージョンは1.54.3
です。
それではやっていく
まずは、Code > Preferences > User Snippets
を選択します。
次に表示されたウィンドウから、スニペットを作成したい言語を選択します。今回は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", // "body": [ // "console.log('$1');", // "$2" // ], // "description": "Log output to console" // } }
書き方はコメントに記載されている通りですが、簡単に解説すると、prefix
にはトリガーとなる文字列を、body
には展開するコードを、description
にはスニペットの説明をそれぞれ書いていく感じです。$1
や$2
などは、展開後のカーソルの位置を設定するための値です。
てな感じで本日も以上になります🍺