VScodeにSnippetを登録する

投稿者: | 2020年5月21日

先週、AtomにSnippetを追加しましたが、なんやかんやでVscodeに移行したのでVScodeにもSnippetを登録します。

AtomでSnippetを追加したときと異なるのは、VScodeの場合jsonの書き方で書かないと行けない点です。Atomのときはcsonだったので、jsonの記述がめちゃくちゃめんどくさいです。

Snippetの追加の仕方は次の通りです。

まず、ファイル→ユーザ設定→ユーザスニペットを選択。macだとcode→基本設定→ユーザスニペットになります。

新規に作成する場合は、どの言語の書く?とズラズラと並んで出てくるので、追加する予定の言語を選択します。

今回はC++なので、C++を選択。すると、cpp.jsonというファイルが開くので、そこに記述していきます。

ファイルを開いとき、コメントアウトされている部分がありますが、全部消すか、下に示す例にようにさらにコメントアウトする必要があります。

書き方はAtomと同じですが、1行ごとに" "で囲って、次に行がある場合は最後に","をつけます。めっちゃめんどくさい。

結果、私の場合は次のようになりました。

{
    /*
    // Place your snippets for cpp 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"
    // }
    "Header":{

    }
}
*/
    "Print to console": {
        "prefix": "header",
        "body": [
            "#include <bits/stdc++.h>",
            "#define rep(i, n) for (int i = 0; i < n; i++)",
            "using namespace std;",
            "using ll = long long;",
            "using P = pair<int, int>;",
            "int main(){",
            "   $1",
            "   return 0;",
            "}"
        ],
        "description": "Header"
    },
    "cout": {
        "prefix": "co",
        "body": [
            "cout<< $1 << endl;"
        ],
        "description": "cout"
    }
}

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください