Share snippets with your team in VS Code

rebornix
rebornix
Mar 17, 2017 · 3 min read

Code snippets are templates that make entering repeating code patterns easier. Visual Studio Code has this feature from the very beginning (before 1.0). If you haven’t given it a try yet, you may want to read our awesome guidance on Creating your Own Snippets first, it will just take your 5 minutes and you’ll love it.

Share Custom Snippets — The Old Way

Snippets in VS Code are defined in a JSON format and stored in a per user file (similar to your User Setting file). If you want to share your awesome code snippets with your team, or even the community with ease, you need create a snippet extension and publish it to VS Code marketplace.

It’s easy and straightforward to make Snippet Extension and we already have more than 400 snippet extensions out there.

However last December, our rock star Johannes made a huge improvement to Code Completion Provider, with which extensions can insert dynamically generated snippets to the editor at runtime. It means you can actually store your snippets wherever you want: user space, share folder, web server, or even inside projects.

To demonstrate that, I create this tiny extension Project Snippets to provide you the ability of Project/Workspace Level Snippet.

Project/Workspace Level Snippet

With this extension, you can put the snippets in your code project, just like the way you share editor/lint/debugging/etc configurations with others working on the same project.

Take VSCodeVim as example, we have a bunch of test cases and they are all in the same shape:

To make writing tests less boring (I mean more fun), I created a snippet to help me insert common codes

Now finally I can move this single-use-case snippet out of my personal snippet file and put it into VSCodeVim’s project snippet folder . Then every contributor can benefit from it and IMHO it may increase our test coverage somehow.

Conclusion

I tried really hard to come up with any drawback of sharing snippets in projects and finally I got one: everyone has to install an extension: Project Snippets.

Hack Visual Studio Code

VS Code Hacker’s Guide: learn how to customize VS Code with…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store