Steve Kinney

Snippet Libraries and Extensions

Snippet Libraries and Extensions

Built-in Snippets

Visual Studio Code comes with a set of built-in snippets for many popular languages. These snippets provide basic code structures and are a good starting point for understanding snippet usage.

  • Accessing Built-in Snippets: To see built-in snippets for a language, open a file of that language type (e.g., .js for JavaScript, .py for Python). Type a common keyword or prefix (e.g., for in JavaScript, def in Python). Visual Studio Code’s IntelliSense will suggest built-in snippets alongside other code completions. Look for the snippet icon in the suggestion list.
  • Exploring Built-in Snippets: You can’t directly edit built-in snippets. However, you can often find their definitions within Visual Studio Code’s extension files if you want to understand their structure or create your own variations. The exact location of built-in snippets can vary depending on the language extension.
  • Examples of Built-in Snippets:
    • JavaScript: for, foreach, function, class, console.log, import.
    • Python: def, class, if, for, while, try.
    • HTML: !, html:5, link, script.
    • CSS: anim, bgc, dis, pos.

Explore the built-in snippets for the languages you use regularly to discover quick shortcuts and understand common code patterns.

Community Snippets: Extensions and Libraries

The Visual Studio Code Marketplace is rich with snippet extensions and libraries created by the community. These extensions offer pre-built snippet collections for various languages, frameworks, and libraries, saving you the effort of creating everything from scratch.

I don’t use any of these.

People like them, but I am way too high-strung and prefer to build my own snippets. Your mileage may vary.

Go Hunting for a Snippets Extension

  1. Open Extensions View: Click on the Extensions icon in the Activity Bar on the side of Visual Studio Code (or press Ctrl+Shift+X / Cmd+Shift+X).
  2. Search for “snippets”: In the Extensions Marketplace search bar, type “snippets” followed by the language or framework you’re interested in (e.g., “snippets javascript”, “snippets react”, “snippets python”).
  3. Browse and Install: Explore the search results. Look for extensions with good ratings, a large number of installs, and recent updates. Read the extension description and features to see if it meets your needs.
  4. Install Extension: Click the “Install” button to install the extension. Visual Studio Code will download and activate the extension.
  • ES7+ React/Redux/React-Native snippets: For React development.
  • Python Snippets: For Python development.
  • Angular Snippets (Version 15): For Angular development.
  • Vue 3 Snippets: For Vue.js development.
  • Go Snippets: For Go development.
  • PHP Snippets: For PHP development.
  • HTML Snippets: Enhanced HTML snippets.
  • CSS Peek: While primarily for CSS peeking, it often includes CSS snippets.

Creating Snippet Extensions

We’ll talk more about extensions later.

But, I figure if you’ve read this far—then this content might be helpful or interesting to you.

If you have created a valuable collection of snippets that you want to share with the wider Visual Studio Code community, consider creating and publishing a snippet extension.

  1. Set up Extension Development Environment: Follow the Visual Studio Code Extension API documentation to set up your extension development environment and install the Yeoman Visual Studio Code Extension Generator (yo code).

  2. Generate Snippet Extension: Use the Yeoman generator to create a new snippet extension project:

    Bash

    yo code

    Choose “New Snippets package” when prompted.

  3. Define Snippets in Extension: Within your extension project, you’ll find a snippets folder. Create .code-snippets files within this folder to define your snippets (e.g., my-language-snippets.code-snippets). Follow the standard snippet JSON format.

  4. Package and Publish: Use the vsce tool (Visual Studio Code Extension Manager) to package and publish your extension to the Visual Studio Code Marketplace. Refer to the Visual Studio Code Extension Marketplace documentation for detailed publishing steps.

Last modified on .