What's The Best Way To Write Multi-Line Code Snippets In VS-Code?

kakyo picture kakyo · Jan 25, 2017 · Viewed 15.7k times · Source

In Sublime Text, multi-line code snippets can be defined with white-spaces in a snippet file.

But as far as I know, VS-Code needs a JSON entry. These require either:

  1. Hard-breaks into a list of double-quoted strings, or
  2. Soft-break a long string using line-breaks \n

This is inconvenient compared to the WYSIWYG approaches other IDEs provide out of the box.

Are there better ways for defining long-blocks of code?

Answer

Armin picture Armin · Apr 1, 2017

You can define the body of your snippet as an array of strings, each beginning on a new line.

Like this:

  "Create for loop":{
    "prefix": "mkfor",
    "body":[
      "for(int i = 0; i < 3; i++)",
      "{",
      "   //code goes here",
      "}"
    ],
   "description": "Creates a for loop"
  }

or if you install Easy Snippet Maker extension, you can create your snippets by highlighting texts.