LoginSignup
1
1

More than 3 years have passed since last update.

[VSCode] Python の print f-string 用のユーザー スニペットを作ってみた

Posted at

Python 3.8 で採用された、print の f-string debugging(print(f"{○○○ = }")という書き方)が気に入っています。
ただ、VSCodeだとIntellisenseを使っても結構タイプ量が多くなるので、自分でスニペット作ってみました。

・ユーザースニペットの作り方
メニュー>「Code」 >「基本設定」>「ユーザー スニペット」を選択>「検索ボックス」で「Python」を検索>「python.json」ファイルが開く

以下の記述を追加します;

python.json
{
    "Print f-string debugging": {
        "prefix": "print",
        "body": [
            "print(f\"{$1 = }\")",
        ],
        "description": "print(f\"{ = }\")"
    },
    "Print with quote": {
        "prefix": "print",
        "body": [
            "print(\"$1\")",
        ],
        "description": "print(\"\")"
    },
}

以下、簡単な解説です;

  • "Print f-string debugging" の名前は、あまり関係ありません。

  • "prefix" が、スニペットを出すトリガーとなる文字です。

  • "body" が、実際に入力される文字です。"\でエスケープする必要があります。また、$1がカーソルの位置になり、$2$3、、はTabキーを押した時に次に移動する位置になります。

  • "description" が、スニペットが表示された時の説明になります。

  • "Print with quote" は、ついでに作ったprint("")のスニペットです。

 
このスニペット、自分的にはかなり愛用しています。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1