How to Execute Code Snippets in VS Code Editor

Visual Studio Code (VS Code) is a popular open-source code editor with robust support for executing code snippets. Code snippets allow you to quickly insert reusable code templates, which can greatly improve coding efficiency.

What are Code Snippets

Code snippets are small reusable pieces of code that can be quickly inserted into an editor via a predefined shortcut. For example, typing for and pressing tab in JavaScript can automatically expand the text into a for loop boilerplate.

Some key benefits of using snippets include:

  • Save time by avoiding repeatedly typing boilerplate code
  • Increase consistency by reusing the same code structures and patterns
  • Improve productivity by reducing context switching between writing and searching sample code
  • Reduce errors by using tried and tested code templates

Executing Snippets in VS Code

VS Code has excellent built-in support for snippets, with no extensions required. Here are the main ways to execute code snippets:

Trigger Keyword Expansion

  • Start typing a predefined snippet trigger keyword (e.g. for, if, class)
  • Press Tab to expand the snippet
  • The cursor will jump to different tab stops allowing you to fill in variable names

Open Snippet Picker

  • Windows/Linux: Ctrl+Space
  • macOS: Cmd+Space
  • Search for snippets by name
  • Select one to insert at cursor position

Insert from Command Palette

  • Windows/Linux: Ctrl+Shift+P
  • macOS: Cmd+Shift+P
  • Search “insert snippet”
  • Pick a snippet from the list

Creating Custom Snippets

VS Code lets you define custom snippets for any language using the JSON format.

To create a new snippet:

  1. Open Command Palette
  2. Search for “Configure User Snippets”
  3. Select the language scope (e.g. javascript, html, markdown)
  4. Add snippet configurations in JSON format

A basic snippet structure looks like:

"Print to console": {
    "prefix": "log",
    "body": [
        "console.log('$1');",
        "$2"
    ],
    "description": "Log output to console"
}

The main properties are:

  • “prefix”: The trigger keyword
  • “body”: Template with $1, $2 for tab stops
  • “description”: Summary of the snippet

See the documentation for more details on creating custom snippets.

Useful Snippet Extensions

Here are some recommended VS Code extensions that provide useful code snippets:

  • ES7 React/Redux/GraphQL/React-Native snippets – Snippets for popular React libraries
  • Bootstrap 4, Font awesome 4, Font Awesome 5 Free & Pro snippets – Snippets for web UI frameworks
  • Markdown All in One – Markdown syntax snippets
  • Python Snippets – Commonly used Python snippets
  • VS Code Snippets – General code and markdown snippets

Search for more snippets extensions in the VS Code Marketplace.

Executing Snippets in Markdown Articles

When writing Markdown documents, snippets can be executed the same way by triggering keyword expansion.

For example, typing heading2 and pressing tab will expand into:

## Heading 2

Many Markdown extensions like Markdown All in One also provide useful Markdown syntax snippets, like tables and images.

Tips for Effective Use of Snippets

Here are some tips:

  • Use snippets for boilerplate code you write often
  • Try memorizing snippet triggers instead of full templates
  • Define custom snippets for your workflow with tab stops
  • Search the Marketplace for snippet extensions for your stack
  • Treat snippets as a starting point before customizing further

Conclusion

VS Code snippets provide a fantastic way to improve coding efficiency and consistency. Both built-in and custom snippets can save time, reduce errors, and speed up nearly any coding workflow. Combined with the rich Marketplace ecosystem, code snippets make VS Code an extremely productive editor for software development.