Today I learned about Visual Studio Code Snippets, these are code templates that save you the time wasted writing repeating code.

To create your own code snippets, select File > Preferences > User Snippets and then select the language you want to create snippets for. You can use the Snippet Generator extension, or this snippet generator webapp (my personal pereference) to help you create the snippets JSON file.


This is a list of snippets that I created for Python 🐍.

{
"Open file in write mode": {
"prefix": [
"open",
"open-write"
],
"body": [
"with open('${1:file.txt}', 'a+', encoding = ${2:'utf-8'}) as f:",
" f.write(${3:data})$0"
],
"description": "Open file in write mode"
},
"Open file in read mode": {
"prefix": [
"open",
"open-read"
],
"body": [
"with open('${1:file.txt}', 'r', encoding = ${2:'utf-8'}) as f:",
" print(f.read())$0"
],
"description": "Open file in read mode"
},
"List comprehension if else": {
"prefix": [
"list-comprehension-if-else",
"lc-if-else"
],
"body": [
"${1:data} = [${2:element} for ${2:element} in ${3:iterable} if ${4:condition}]$0"
],
"description": "List comprehension with if else condition"
},
"List comprehension if": {
"prefix": [
"list-comprehension-if",
"lc-if"
],
"body": [
"${1:data} = [${2:element} for ${2:element} in ${3:iterable} if ${4:condition}]$0"
],
"description": "List comprehension with if condition"
},
"List comprehension": {
"prefix": [
"list-comprehension",
"lc"
],
"body": [
"${1:data} = [${2:element} for ${2:element} in ${3:iterable}]$0"
],
"description": "List comprehension"
},
"if": {
"prefix": [
"if",
"if-statement"
],
"body": [
"if ${1:condition}:",
" ${2:expr}$0"
],
"description": "If statement"
},
"if else": {
"prefix": [
"if-else",
"if-else-statement"
],
"body": [
"if ${1:condition}:",
" ${2:expr}$0",
"else:",
" ${3:expr}"
],
"description": "If else statement"
},
"for": {
"prefix": [
"for",
"for-loop"
],
"body": [
"for ${1:element} in ${2:iterable}:",
" ${3:expr}$0"
],
"description": "For loop"
},
"while": {
"prefix": [
"while",
"while-loop"
],
"body": [
"while ${1:condition}:",
" ${2:expr}$0"
],
"description": "While loop"
},
"try except": {
"prefix": [
"try-except",
],
"body": [
"try:",
" ${1:expr}",
"except ${2:Exception} as e:",
" ${2:print(str(e))}$0"
],
"description": "Try except statement"
},
"try except else": {
"prefix": [
"try-except-else",
],
"body": [
"try:",
" ${1:expr}",
"except ${2:Exception} as e:",
" ${2:print(str(e))}$0",
"else:",
" ${3:expr}"
],
"description": "Try except else statement"
},
"try except finally": {
"prefix": [
"try-except-finally",
],
"body": [
"try:",
" ${1:expr}",
"except ${2:Exception} as e:",
" ${2:print(str(e))}$0",
"finally:",
" ${3:expr}"
],
"description": "Try except finally statement"
},
"lambda": {
"prefix": [
"lambda",
"lambda-function"
],
"body": [
"lambda ${1:args}: ${2:expr}$0"
],
"description": "Lambda function"
},
"for enumerate": {
"prefix": [
"for-enumerate",
"enumerate-loop"
],
"body": [
"for ${1:idx}, ${2:element} in enumerate(${3:iterable}):",
" ${4:expr}$0"
],
"description": "For enumerate loop"
}
}
view raw python.json hosted with ❤ by GitHub