#VisualStudioCode – Create and use Code Snippets 🗒️ in @code, create custom snippets just for a workspace, and for multi folder solutions

Hi !

Update: I wrote this post last year for my Python series. This is an update focused on scenarios also working with C++ and Platform IO.

Code Snippets are super useful, and create your custom Code Snippets is super easy in Visual Studio Code. The official Documentation (see references) explains how to create snippets, with the full details of the JSON file, sections, contents and more. However there are some extra tips on top of this information that are super useful.

Snippet Creator

This is an amazing Visual Studio Code Extension. Just search for [snippet creator] on the Extension and install the extension.

visual studio code snippet creator extension

Once the extension is installed, the way to use this is super simple.

  • Select some code
  • CTRL + SHIFT + P, and type [create snippet]
  • Follow the steps, define snippet prefix, and optionally description
  • That’s it!
visual studio code snippet creator add new snippet

I created a custom snippet with the prefix [cameraCalcFPS] and now the snippet is available for any python file !

visual studio code custom code snippet available

In order to check all the custom code snippets

  • CTRL + SHIFT + P
  • Type [configure user snippets]
  • Open the specific one, for this sample python.json

The code snippets file has all the custom created snippets

visual studio code open custom snippets for python

Workspace specific snippets

There is also another specific scenario which I found very interesting:

The need for specific snippets inside a workspace

In order to do this, just create a file [mysnippets.code-snippets] and paste your own snippets here. This snippets will be only available for this workspace!

snippets for a workspace

Code Snippets for Multi Folder projects

In the current scenario, I already created a snippets file [wiosnippets.code-snippets] with code snippets for Platform IO and Wio Terminal projects.

I have several Platform IO projects in different folders, and I open the top folder. In order to use these snippets, I will create a new folder named [.vscode] and copy the [wiosnippets.code-snippets] file to this folder.

folder with several c++ projects and vscode folder with the c++ custom snippets

Once I reload the Window in Visual Studio Code, I can start to use my snippets in any cpp file !

snippets available for all projects in the current visual studio code session

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.


References

1 comment

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: