LoginSignup
0
1

More than 1 year has passed since last update.

VSCodeでコードユニペット(Python)を登録する - 備忘録

Last updated at Posted at 2022-01-18

#目的
Pythonのプログラムを書く際にお約束の文言を毎回入力するの面倒なので,コードユニペットに登録してしまおうというヤツ
##こんな感じに
『shebang』って打とうとすると...
スクリーンショット 2022-01-18 16.17.37.png
予測候補が表示されて
スクリーンショット 2022-01-18 16.18.44.png
シバンが入力される
同様に『main』って打とうとすると...
スクリーンショット 2022-01-18 16.20.05.png
予測候補が表示されて
スクリーンショット 2022-01-18 16.21.10.png
↑が入力される

まぁ便利!!

##実際に登録してみる

  1. VSCodeの基本設定からユーザユニペットを選択
    スクリーンショット 2022-01-18 16.03.26.png

  2. 『Python』を選択(デフォルトであるはずなので,新しく作る必要はない)
    スクリーンショット 2022-01-18 16.31.00.png

  3. そうしたら,jsonファイルが開かれてこんな内容が書かれているはず
    スクリーンショット 2022-01-18 16.34.14.png

  4. ここに,下の内容を追記する ※既にある『{ }』内に記載すること!!

    python.json
    "main":{
    	"prefix": "main",
    	"body": [
    		"if __name__ == '__main__':",
    		"    ${1:pass}"
    	],
    	"description": "main"
    },
    "shebang":{
    	"prefix": "shebang",
    	"body": [
    		"#!/usr/bin/env python3",
    		"# coding: utf-8"
    	]
    }
    
  5. 最終的に,自分のpython.jsonファイルはこんな感じの内容になった

python.json
{
	// Place your snippets for python 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"
	// }

	"main":{
		"prefix": "main",
		"body": [
			"if __name__ == '__main__':",
			"    ${1:pass}"
		],
		"description": "main"
	},
	"shebang":{
		"prefix": "shebang",
		"body": [
			"#!/usr/bin/env python3",
			"# coding: utf-8"
		]
	}
}
0
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
0
1