Custom Snippets in VS Code
Tags: productivity, vscode, codenewbie, beginners
Published: 2021-07-12 04:48:39

## Tired of writing the same code over and over again?
or
## Want to save time in a Competitive Programming contest?

Rest Assured as VS Code allows you to create your very own code snippets to save time while programming.

What is a Snippet?
A prefix is what is used to trigger the snippet and the body will be expanded and inserted.

You can create snippets:

* Globally- which will be used for all programming languages across all folders.

* Project- custom snippets for each project separately.

* Language- create templates for each programming language you code in separately.

Not only that you can create multiple code snippets and use different prefixes to use your snippet of choice whenever you want.

Now let's dive right into it and see how we can do all of this:
**open VS Code > settings > user snippets**
            
You should see a drop-down menu as shown in the image below.

![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ei14efjbjqviziyp44cg.png)

Now select the particular language for which you want to create the snippets by scrolling or searching for it (by language identifier) in the search box shown or you can also select the New Global Snippets file if snippet should appear for all languages. 

Here I would be showing how to create 2 custom snippets for C++ :
* One that I use for normal programming.
* Another one that I use for competitive programming.

Select CPP(C++) from the drop-down menu for C++. You'll see text commented in between 
a pair of curly braces { // }. The commented text contains some basic instructions on how
to create the snippet and an example code, you can either clear all of it or edit and use the same code.

## Normal Programming Code Snippet
here is how it looks in the `cpp.json` file 
( I'll add the screenshot of the complete page below)

![2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc27d2ai63bnq0up3rq2.png)

the "snippet-normal-c++" is the title you choose for your snippet

**prefix**: the word by which access your snippet while coding (basically when you type as in this case **"cpp"** while coding in a `.cpp` file  vs code Intellisense automatically gives you suggestions from where you can hit enter and get your boilerplate code)

you can see the suggestions vs code gives in the image below:

(try this after creating your snippet and saving it)

![3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qth2sj6jgvr5xbvwpv7l.png)

**body**: this is where you write the code template you want, you can write them in between double quotes " " and separate them by using a comma(,) which is the typical syntax of a `.json` file.

**description**: you can add a little description if you want which will be displayed by the vs code IntelliSense.
Now one particular thing you might have noticed is the "$1" symbol which I have used.
That indicates the position where the cursor will be placed after the template has been copied.

Here is how the template looks like when used while coding -

![4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imyho2ugpkvlsr5y4dux.png)


## Competitive Programming Code Snippet

Why use `#include <bits/stdc++.h>` ? you might ask

 It is a header file that includes every standard library. In programming contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time sensitive

Also taking input of the number of test cases is a part of each question so that is why I have included that in the template I use.

But anyways that was me. You can design your code that fulfills your needs.

here is how the output of this snippet looks like:

![6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qilm5ra6h3gcp5d3szrt.png)

Here is how the Competitive Programming Snippet looks like:

![5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c30w8inxfn2tht8u7b1l.png)

And here is how the complete `cpp.json` file looks like:

![7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcq85eef0wlke84mltva.png)

Hit **Ctrl+S** to save the settings... 

You can add as many templates as you want and fine-tune them according to your need.

So what are you waiting for? Go and create your snippet right now. KEEP CODING !!!

If you prefer to watch videos to learn something here is a [YouTube](https://www.youtube.com/watch?v=cVBM5Bn5yjI) video I found helpful while learning how to create custom snippets:

{% youtube cVBM5Bn5yjI %}

There are many more features that you can add to your template and even download some extensions for that. To know more about that you can read Snippets in [Visual Studio Code](https://code.visualstudio.com/docs/editor/userdefinedsnippets).

### If you liked my content consider following me on [Twitter](https://twitter.com/cenacr007_harsh)

Also if you got any questions feel free to ping me on Twitter.

### Thank You! 😊