Skip to content

Instantly share code, notes, and snippets.

@MichaelCurrin
Last active March 26, 2024 15:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MichaelCurrin/26d7ac563b875f02ee48cfbfc21cd3c0 to your computer and use it in GitHub Desktop.
Save MichaelCurrin/26d7ac563b875f02ee48cfbfc21cd3c0 to your computer and use it in GitHub Desktop.
VS Code snippets

VS Code Snippets

Customize VS Code with some code snippets so you can insert them into your projects

For more info see:

Snippet samples

Click the link to jump to that section of this gist.

Instructions

How to set up and use the the base HTML snippet as an example.

Set up snippet

  1. Open VS Code.
  2. Open the command palette. ("View" then "Command Palette").
  3. Type "Snippet" to narrow the options.
  4. Select the option "Preferences: Configure User Snippets".
  5. Select html.json.
  6. Delete the contents of the file.
  7. Copy the content of the html.json file in this gist and paste it in the file.
  8. Save it.

You can have more than HTML snippet, but in this case we only set up one. See the ignore.json example for supporting multiple snippets.

Use snippet

We set "prefix": "html_base" in the file, so that defines the snippet trigger we use here.

  1. Open or create a new file which has a .html extension. Or select the format in the bottom right and choose HTML.
  2. Left-click in the file where you want the snippet to be inserted.
  3. Start typing the snippet trigger.
    • Type html_base and s
    • Or just html, then find html_base from available options.
  4. Press Enter or Tab.
  5. The snippet will be inserted.
  6. This particular snippet has some placefolders.
    1. Start typing immediately to enter a title.
    2. Press Tab to skip to the next placeholder.
    3. Enter a description.
    4. Press Tab to skip to the next breakpoint, which will be in the body here. But in a simpler snippet you'd jump to the end of the snippet.

Another way to insert a snippet is using "Insert snippet" in the command palette, so see any snippets availble for the current file format.

{
"Editor config base": {
"prefix": "editorconfig",
"body": [
"root = true",
"",
"[*]",
"indent_style = space",
"indent_size = 2",
"",
"[*.{md,html}]",
"indent_size = 4",
"",
"[Makefile]",
"indent_style = tab",
"indent_size = 4"
],
"description": "Template for a .editorconfig file"
}
}
{
"Minimal HTML base": {
"prefix": "html_base",
"body": [
"<!DOCTYPE html>",
"<html lang=\"en\">",
"",
"<head>",
" <meta charset=\"utf-8\">",
" <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\">",
" <meta content=\"IE=edge\" http-equiv=\"X-UA-Compatible\">",
"",
" <title>${1:TITLE}</title>",
" <meta name=\"description\" content=\"${2:DESCRIPTION}\">",
"",
" <link href=\"styles.css\" rel=\"stylesheet\">",
"",
" <script defer src=\"main.js\"></script>",
"</head>",
"",
"<body>",
" <div>",
" <h1>${1:TITLE}</h1>",
"",
" <p><i>${2:DESCRIPTION}</i></p>",
"",
" $3",
"",
" </div>",
"</body>",
"",
"</html>"
],
"description": "Create base contents of an HTML file"
}
}
{
"JavaScript": {
"prefix": "js",
"body": [
"node_modules/",
"",
"build/",
"",
"npm-debug.log*",
"yarn-debug.log*",
"yarn-error.log*",
],
"description": "Content for .gitignore file in a Node / JavaScript project."
},
"Jekyll": {
"prefix": "jekyll",
"body": [
".jekyll-cache/",
".sass-cache/",
".jekyll-metadata",
"",
".bundle/",
"vendor/",
"",
"_site/"
],
"description": "Content for .gitignore file in a Jekyll project."
}
}
{
"Jest test": {
"prefix": "jest_test",
"body": [
"describe(\"description here\", () => {",
" describe(\"#foo\", () => {",
" it(\"returns fizz when called with buzz\", () => {",
" expect(foo('buzz')).toBe('fizz')",
" })",
" })",
"})"
],
"description": "Outline of a unit test with Jest"
}
}
{
"Makefile generic": {
"prefix": "makefile_generic",
"body": [
"default: install install-dev",
"",
"all: hooks install install-dev check build",
"",
"",
"h help:",
"\t@grep '^[a-z]' Makefile",
"",
"",
".PHONY: hooks",
"hooks:",
"\tcd .git/hooks && ln -s -f ../../hooks/pre-push pre-push",
"",
"install:",
"\t@echo \"TODO: Add your core package install step\"",
"install-dev:",
"\t@echo \"TODO: Add your dev packages install step\"",
"",
"upgrade:",
"\t@echo \"TODO: Add your package and dev packages upgrade step\"",
"",
"",
"fmt-fix:",
"\t@echo \"TODO: Add format fix step\"",
"fmt-check:",
"\t@echo \"TODO: Add format check step\"",
"",
"lint-fix:",
"\t@echo \"TODO: Add lint fix step\"",
"lint-check:",
"\t@echo \"TODO: Add lint check step\"",
"",
"test:",
"\t@echo \"TODO: Add unit tests step\"",
"",
"fix: fmt-fix lint-fix",
"check: fmt-check lint-check test",
"",
"",
"run:",
"\t@echo \"TODO: Add run step\"",
"",
"",
"s serve:",
"\t@echo \"TODO: Add dev server step\"",
"",
"",
"build:",
"\t@echo \"TODO: Add production build step\"",
"\tmkdir -p build",
"\t@echo 'Production-ready app' > build/app"
],
"description": "Generic Makefile template"
}
}
{
"License template": {
"prefix": "license_mit",
"body": [
"MIT License",
"",
"Copyright (c) ${1:2021} ${2:MichaelCurrin}",
"",
"Permission is hereby granted, free of charge, to any person obtaining a copy",
"of this software and associated documentation files (the \"Software\"), to deal",
"in the Software without restriction, including without limitation the rights",
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell",
"copies of the Software, and to permit persons to whom the Software is",
"furnished to do so, subject to the following conditions:",
"",
"The above copyright notice and this permission notice shall be included in all",
"copies or substantial portions of the Software.",
"",
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR",
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE",
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE",
"SOFTWARE."
],
"description": "Create contents of a license file"
}
}
{
"Generic Actions workflow": {
"prefix": "workflow_generic",
"body": [
"name: Placeholder CI",
"",
"on:",
" push:",
" branches: main",
" paths-ignore:",
" - \"docs/**\"",
"",
" pull_request:",
" branches: main",
" paths-ignore:",
" - \"docs/**\"",
"",
"jobs:",
" build:",
" runs-on: ubuntu-latest",
"",
" steps:",
" - uses: actions/checkout@v2",
"",
" - name: Set up Python / Node / Ruby / Go / Rust / etc.",
" run: echo 'Set up environment here'",
"",
" - name: Install dependencies",
" run: make install",
"",
" - name: Install dev dependencies",
" run: make install-dev",
"",
" - name: Check formatting",
" run: make fmt-check",
"",
" - name: Lint",
" run: make lint-check",
"",
" - name: Run unit tests",
" run: make test",
"",
" - name: Build",
" run: make build"
],
"description": "Template for a GitHub Actions workflow"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment