How to Create Your Own VS Code Extension: A Simple, Step-by-Step Guide
Tags: vscode, webdev, beginners, tutorial
Published: 2021-10-23 05:03:30

Section 1: What to keep in mind before creating an extension


1.1. VS Code has an extensive API, and most of the most common extensions such as Plugins (which lets you integrate extension functionality with your code), are implemented using its C# API.



1.2. However, some functionality which is currently implemented by extensions can be handled in different ways, such as importing files and helping users with syntax highlighting. Using some extensions can be an implementation detail, and hence, be added to your project in a way that can’t be changed later.



1.3. For each extension that is implemented, you should know its API so that you can use it to implement your own extension. This includes the extensions for the core editor UI, code language UI and some advanced extensions.



How to find out what extensions are already available


How to contribute



The key point here is that Visual Studio Code is built to be extensible, built by, and for, you. Add to that the fact that the code editor is open source (or in this case contributed back) and that there are a bunch of extensions already available, it’s easy to see how VS Code is designed for you to contribute to and get your own extensions made. But what exactly can I do in Visual Studio Code?



Extension Authoring



A simple example of an extension is a notification system. Although Visual Studio Code has many built-in features to accomplish this, for this example we’re going to build an extension from scratch. Here’s the problem: I want to be notified whenever my code works as expected, but it’s hard to know if my code is working as expected until I see a log.



How to create a VS Code extension


We have assembled this post to answer all your questions about building extensions for Visual Studio Code. Before starting this post, I would like to mention that most of the information presented in this post is for Visual Studio Code 1.5 or later.



The post covers all the steps involved in creating your own VS Code extension. This post will go over the entire journey that you will go through from signing up and activating your extension, installing the extension in Visual Studio Code, adding user-defined functions, extending VS Code, creating a command line script to install, and finally, to uninstall the extension and remove the package.



Create a New Extension Project



To create a new extension project, go to Extension Projects > New > Create New > Extension.



Using the VS Code Quick Open command to open your project folder


An awesome developer experience all wrapped up in a streamlined package



Extensions are perfect for automating tasks, right? Great! But how do you go about writing a VS Code extension for something as simple as viewing the contents of a folder?



This week, I’ll be walking you through the steps necessary to do just that. I’ll introduce the build process you can follow if you want to go for it on your own, and provide a full preview of the extension I created.



Prerequisites



Nothing below assumes you know what you’re doing. In fact, I’m hoping you’ll share the extension that we build and have a chance to show it off! I’ll feature your extension on this blog, too!



I’ll be using Visual Studio 2015 Community Edition (included with VS 2015 RTM) on an OSX machine.



Create a Package.json file to list project dependencies


Package.json file to list project dependencies Create an extension.json to define extensions dependencies



Extension.json to define extensions dependencies Add ASP.NET Core dependencies



Adding ASP.NET Core dependencies Start Visual Studio Code



Development Steps



To create a package.json file, first you will need to configure Visual Studio Code to use it. Next, if your Package.json doesn’t already exist, create a new one by going to Settings | Project | Generate New Project . Copy the contents of the contents of the below.json file and paste it into the new project’s root folder:



{ "name": "Hello VS Code Extension", "version": "1.0.0-dev", "description": "A simple, step-by-step guide.



Add a tsconfig.json file for TypeScript compilation


For all things Visual Studio Code related, read on!



Let’s take a look at the process of creating an extension.



Step 1: Build a Visual Studio Code extension



First, let’s explore how to build Visual Studio Code extensions. For the purpose of this post, a Visual Studio Code extension is a .vscode file, which has the extension .vscode. As of Visual Studio 2017 RC and the next Visual Studio Update, extensions can be used for installing, managing and editing .vscode projects. If you want to create an extension for a version of Visual Studio other than Visual Studio 2017, you have to add the below components to your extension project.



Add the source files for your VS Code extension, e.g., .vscode/*.*


Write a component definition with your extension



Create the extension once, build and share the executable with friends



Do the same with the Maven-based .vscode-maven



Step 1: Create a New VS Code Extension



Let’s start with creating a simple extension — let’s create an extension that allows me to delete an item from an ItemGroup tree. First, create a new VS Code Extension.



Step 2: Write a Component Definition



In order to build a component definition for your extension, you must write code that defines a component class. This class represents a container that all of your components will be added to. This component will also define how the application will be able to register these components.



Run your extension locally by using npm start or yarn start 


Why is there so much choice for VS Code extensions? And are any of the extensions useful?



Working with and customizing extensions



Want to use a new input style, different syntax highlighting, use new features?



Lets you target multiple devices and languages without leaving your editor



You can even write your own extensions for your own IDE (or your IDE can be your extension)?



Reasons why VS Code extensions are useful



While this guide is targeted to beginners, most of these steps should be very intuitive for seasoned developers.



Publish or share your extension with others


Extend VS Code with extensions for team collaboration and mobile code



Check out the Visual Studio Code Add-on Directory to browse a large collection of plugins available for VS Code.