Skip to content
Related Articles

Related Articles

Improve Article
Creation and Utilisation of Custom Snippets for Competitive programming in VScode
  • Difficulty Level : Medium
  • Last Updated : 12 Mar, 2021

Coding down the whole algorithm from scratch during the online contest can cost you plenty of time and may lead to a bad rank. There is no point in coding the same algorithm you have coded hundreds of times earlier. Why not utilize your previously coded implementations? You can do that by creating custom code snippets and summoning them just by a keyword whenever and wherever you need.

Code Snippets Creation:

  • Open folder where you code in VScode as shown in the below image. Here we will make a code snippet of the Rabin-Karp file and will summon it later in the test file.

Folder Structure

  • Install  Convert text/code to a snippet vs code extension from the marketplace by clicking the square box present in the left side-bar. This extension helps to create JSON files from code and this JSON file is required to make custom snippets in vs code.

VS Code Extension

  • Now,  open the command pallet using command ctrl+shift+p and type reload window in the pallet, and hit enter to reload the window.
  • Copy the code you want to make the snippet for and hit ctrl+shift+p and type convert to snippet in the pallet and hit enter. Fill in the details asked such as snippet title, description, and prefix. Choose your prefix carefully, It will be used to summon this code snippet.
  • Copy the JSON output displayed by extension.

JSON Output

  • Click on the setting wheel present at the bottom in the left side-bar. Choose the user snippet option and then choose the programming language of the snippet. Remove the existing comments and code and put the copy JSON in a curly bracket. You can make as many snippets as you want, just separate the two consecutive snippets JSON with commas.

Snippet JSON

Code Snippet Utilization:

  • Open a file (test.c++ in my case), type the code snippet prefix you assigned, and hit enter on suggestion. Your file will be populated with the corresponding code snippet.

Code Snippet Recommendation

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up
Recommended Articles
Page :