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.
This is an amazing Visual Studio Code Extension. Just search for [snippet creator] on the Extension and install the 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!
I created a custom snippet with the prefix [cameraCalcFPS] and now the snippet is available for any python file !
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
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!
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.
Once I reload the Window in Visual Studio Code, I can start to use my snippets in any cpp file !