DEV Community

Cover image for Create custom code snippets in VsCode
DrSimple
DrSimple

Posted on

7 1

Create custom code snippets in VsCode

Programming, in itself, can become very repetitive no matter how **DRY **you aim to be.

dry

To address this, we must learn how to reduce the number of keystrokes we type. I will show you how to create a custom snippet in VS Code in just four steps.

  1. Press Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (Mac).

Image description

  1. Type Preferences: Configure User Snippets and select it.

Image description

  1. Choose the language for the snippet (I will choose typescript.json for TypeScript).

Image description

  1. Paste the following configuration into the snippet file to create a console.log snippet:
{
"Console Log": {
    "prefix": "cl",
    "body": [
      "console.log($1);", // $1 possible variable placeholder
      "$2", // for tab stop i.e. to place cursor,
      "$0" // for final cursor position
    ],
    "description": "Console log snippet"
}
Enter fullscreen mode Exit fullscreen mode
  • "prefix": This is the shortcut you type (e.g., cl).
  • "body": This is the code that will be inserted. $1 is a placeholder for where your cursor will be placed after expanding the snippet.
  • "Description": A short description of the snippet (optional).

Testing

To test, create a new .ts file and type cl anywhere.

Image description

Other examples include:

Function Declaration

{
  "Function Declaration": {
    "prefix": "fn",
    "body": [
      "function $1($2) {",
      "  $3",
      "}",
      "$0"
    ],
    "description": "Function declaration snippet"
  }
}
Enter fullscreen mode Exit fullscreen mode

Try-Catch Block

{
  "Try-Catch Block": {
    "prefix": "tc",
    "body": [
      "try {",
      "  $1",
      "} catch (error) {",
      "  console.error(error);",
      "  $2",
      "}",
      "$0"
    ],
    "description": "Try-catch block snippet"
  }
}
Enter fullscreen mode Exit fullscreen mode

Import Statement

{
  "Import Statement": {
    "prefix": "imp",
    "body": [
      "import $1 from '$2';",
      "$0"
    ],
    "description": "Import statement snippet"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now that you know this, you can work smarter, finish your tasks, and go on vacation.

vacation

Embedded BI Dashboards are 💩 Building them yourself is 💩

Embedded BI Dashboards are 💩 Building them yourself is 💩

Use our developer toolkit to build fast-loading, native-feeling dashboards for your customers (without the sh*t).

Get early access

Top comments (0)

Developer-first embedded dashboards

Developer-first embedded dashboards

Embed in minutes, load in milliseconds, extend infinitely. Import any chart, connect to any database, embed anywhere. Scale elegantly, monitor effortlessly, CI/CD & version control.

Get early access

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!