Guy
Guy

Reputation: 163

VSCode - autocomplete for loops

I used to write C code with Visual Studio, and whenever I wrote "for" and then pressed TAB, it was automatically completed to an entire for loop, i.e.

for (size_t i = 0; i < length; i++)
{

}

Is there a way to enable that in VSCode as well? Even by using some extension? Thanks!

Upvotes: 2

Views: 2516

Answers (1)

Alan
Alan

Reputation: 1

Is there a way to enable that in VSCode as well?

Yes, you can add snippets and customize them according to your needs if the corresponding snippet is not already available, as shown below for the for loop shown in your question.

Step 1

Go to Files -> Preferences -> User Snippets

Step 2

After clicking on the User Snippets you will be prompted with a menu with different options as shown in the screen shot attached. Click on the option saying: New Global Snippets File

image 1

Step 3

When you click on New Global Snippets File, a file will open where you can add your required snippet. Since you've given the for loop that you want in C++, I will write the content that you want to place in that file:

{
  "For Loop": {
    "prefix": ["for", "for-const"],
    "body": ["for (size_t i = ${1:0} ;i < ${2:length}; i++)", "{\t${0://add code here}", "}"],
    "description": "A for loop."
  }
}

Step 4

Save this file with the content shown above and then you will be able to use this snippet. For example, next time you write for you will be prompted with different options and you can press TAB for selecting that option and that snippet will be used at that point, as shown in the below screenshot :

sc 2

sc3

Upvotes: 7

Related Questions