Make life easier with Visual Studio Code Snippets

Snippets in Visual Studio code are templates that make it easier to enter repeatable code patterns or blocks. They’re a helpful timesaver when developing applications. I have found significant time savings within Business Central with Snippets for page and table field templates.

Visual Studio Code has several built-in Snippets, extension publishers may include Snippets, and you can also define your own.
You can see a list of the snippets by the Insert Snipper command in the command palette or by typing one of the triggers for IntelliSense.

Snippets are in JSON, and you can define the Snippet scope as global or project. Each file can contain an unlimited number of snippets.

A Snippet is a named element containing a prefix, body, and description. The prefix of the Snippet defines the trigger words used to display the Snippet in IntelliSense. The body has the actual code for the Snippet, and the description includes the text shown in IntelliSense for the Snippet.

Within the body of the Snippet, you can define TabStops, where the editor cursor will move within a Snippet. You use $1, $2, etc., to specify the cursor location, with $0 being the last TabStop.

PlaceHolders are similar to TabStops, with the addition of a default value. The syntax for a placeholder is similar to the TabStop, with the addition of the default value ${1:Value}. PlaceHolders can be nested: {$1:Value1 ${2:Value2}}

Visual Studio Code contains a range of Variables for use within the body of the Snippet. There are editor Variables for inserting file information, date/time information, and some random values. One of my favorite variables is the Clipboard’s contents with the ${CLIPBOARD}, another is the currently selected text  ${TM_SELECTED_TEXT}.

You can create your Snippet by selecting – Configure User Snippets from the settings menu and selecting either New Global Snippet or a New Project Snippet from the Command Palette. Once you have a Snippet file, you can also edit that directly.

For more information visit Snippets in Visual Studio Code

Note: The code listed in this article is for informational and demonstration purposes only. 

Leave a Reply

Your email address will not be published.