Custom C++ User Snippet in Visual Studio Code
Last Updated :
20 Jan, 2023
Snippets are the small portion of re-usable code wrapped in some prefix word, It allows programmers to bind a particular block of code in a single prefix keyword. Visual Studio Code is a Lightweight and powerful source code editor available in Windows, Linux, and macOS, It comes with many built-in snippets for the loops, control statements, and some keywords. But you know, we can also create our own custom code snippets for time saving and avoiding writing the same piece of code again and again. Users can code faster and productively with the help of custom code snippets.
Existing Snippet in VS Code
Built-in the snippet for the loop.
Now let's take a look at how we can create our own code snippets for c++ language.
Steps need to be performed.
Here we were going to create a user snippet for the C++ language header template, In the Windows machine.
Step 1: Open the Visual Studio Code, and go to the 'Manage' (Gear icon in the bottom left corner).

Step 2: From opened options, Select the 'User Snippets' option.

Step 3: Select your programming language in which you are going to create a snippet. (Here we are selecting C++).

Step 4: Now, one cpp.json file will open Where we have to write a code for our code snippet. First, un-comment the code present below their instructions. (refer to screenshot).

Step 5: In this step, we have to do appropriate changes to their code.
First, Let's understand the meaning of terms present in the code. Snippets generally have four main properties.
- Print to console: This is the word, which will open our snippet when we call it.
- Prefix: This prefix is used when selecting the snippet in intellisense.
- Body: In the body we are writing our main snippet code.
- Description: In this we have to mention brief description of snippet for our reference only.
Note -
- In body '$1' sign determines the position of our cursor when the snippet is activated in code (Tab Stop).
- Note that while writing snippet code in the body, For multiline snippets, the body becomes an array with each line of the snippet becoming a string in that array.
- To format our code properly, we're adding a "\t”, “\n” to lines for proper Indentation.
- To enter special characters like " ", \ we precede them with a " \ ", like " \" " to print "
Our C++ Template Code Snippet

In our snippet, we have used a 'boilerplate' as 'print to console', which means when we type boilerplate and press Enter, our snippet is executed.
Prefix gave as 'boilerplate code'. This will visible while typing the name of the snippet.
The prefix is visible while writing snippet name
In the body, we have included iostream header file and template code for c++. A description of the snippet is also added. "\t” and “\n” are added for proper formatting of code.
This is the snippet code result.
The tab is on the 6th line with four spaces.
Snippet Outcome
C++
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Video Demo:
Here all the above-mentioned steps are performed creating a Snippet. This will give you a clearer understanding.
That's all. In this way, you can create a user snippet for different languages in Visual Studio Code. Each snippet is associated with a particular name, When we type that name and hit the enter key, Snippet code is executed.
Similar Reads
How to Set corresponding File Icon in Visual Studio Code?
You can change the design of the folder icons using the Visual Icon Theme extension. Within VS Code, you can browse and install extensions. Click the Extensions icon in the Activity Bar on the side of VS Code or the View, as we demonstrated below. In this article, we are going to see, how to enable
1 min read
How to Use Cursor AI in Visual Studio Code: Complete Step-by-Step Guide
In the fast-paced world of software development, AI-powered tools are transforming how developers write, debug, and manage code. One standout tool, Cursor AI, integrates seamlessly with Visual Studio Code (VS Code), enhancing productivity with intelligent code completion, context-aware suggestions,
9 min read
Debug Keyboard Shortcuts For Visual Studio Code
Efficient debugging is an essential skill for any developer, and Visual Studio Code offers powerful keyboard shortcuts to streamline the debugging process. Using debug keyboard shortcuts in Visual Studio Code allows you to quickly run, pause, and step through your code, making troubleshooting faster
3 min read
How to create snippets in Sublime Text
In this article, we will discuss how to create snippets in Sublime text for Competitive Programming. In Competitive Programming, time is the most important key, and snippets give us functionality to not write the same code again and again and increase our performance in live contests. To install Sub
2 min read
General Keyboard Shortcuts For Visual Studio Code
Using Visual Studio Code keyboard shortcuts can significantly enhance your productivity and efficiency. Whether you're new to the platform or looking to improve your workflow, knowing the most common keyboard shortcuts in Visual Studio Code will help you navigate and edit code faster. This guide cov
3 min read
How to Install and Setup Live Server Extensions on Visual Studio code?
Visual Studio Code is one of the most sought-after IDE (Integrated Development Interface) of the current time. It helps the developers to efficiently write, build and test software over a single platform by combining and providing a multilateral set of functions. Visual Studio Code supports various
3 min read
How to Indent Code in Vim Editor?
Vim is a popular text editor used by many developers for writing code, creating readme files, and writing scripts. It's open-source and offers a lot of ways to customize your experience. This makes it easier and more efficient to work with. Properly formatting and indenting your code is important be
4 min read
How to enable/disable Word Wrap in Visual Studio Code?
When any text runs off the side of the screen, the Toggle Word Wrap feature of Visual Studio Code consolidates it within the window and formats it perfectly. Whether your property values are getting extremely stretched out from the screen or your comments are flying off to the right, this feature is
1 min read
Convert std::string to LPCWSTR in C++
C++ provides us a facility using which one can represent a sequence of characters as an object of a class. This class is std::string. Internally, std::string class represents a string as a sequence of characters (bytes) and each character can be accessed using the [] operator. The difference between
2 min read
A Step-by-Step Guide to Understanding Code Timeline In VS Code
Visual Studio Code (VS Code) is one of the most popular code editors among developers due to its flexibility, open-source nature, and wide range of features. One of the lesser-known yet powerful features is the Timeline View, which was introduced to help developers explore their project history effi
6 min read