Create Your First Visual Studio Code Extension

When Visual Studio Code doesn't do what you want it to, you install an extension. When you can't find an extension that does exactly what you need, you create your own! In this article, we will look at creating your first Visual Studio Code extension.

The Visual Studio Code team created a generator for creating extensions. This generator will generate all of the necessary starter files to begin creating your extension, which makes the process much easier.

Visual Studio Code team created a generator to make it easy to get started writing extensions!

To get started, you will need to have Yeoman installed, which is a scaffolding tool. According to their docs (screen below), you can install Yeoman (assuming you have Node.js installed), by running

npminstall -g yo

With Yeoman installed, now you need to install the specific generator for Visual Studio Code extensions.

You are now ready to create your first extension! To do so, run the following command.

yo code

You will then answer several questions about your project. You will need to choose what kind of extension you are creating and between TypeScript and JavaScript. We will be choosing JavaScript for this one.

Then you've got a few more questions.

name

identifier

description

type checking (yes)

do you want to initialize a git repository (yes)

After this process is complete, you've got all of the files you need to get started! Your two most important files are...

package.json

extension.js

Open the package.json and let's take a look. There's the typical stuff that you're used to like name, description, etc. But there are 2 more sections that are very important.

activationEvents - this is a list of events that will activate your extension. Extensions are lazy loaded so they aren't activated until one of these activation events occur

commands - list of commands that you provide the user to run via your extension

We will come back to these shortly!

Free Node eBook

Build your first Node apps and learn server-side JavaScript.

📧

Nice!

Check your email to confirm your subscription.

You can also take a look at the extension.js file. This is where we are going to write the code for our extension. There's some boilerplate code in here, so let's break it down.

In the highlighted line below is where our command is being registered with VS Code. Notice that this name "extension.helloworld" is the same as command in the package.json. This is intentional. The package.json defines what commands are available to the user, but the extension.js file actually registers the code for that command.

In this Hello World example, all this command will do is display a Hello World message to the user.

Before we work on code, let's take one more look at the activationEvents section in the package.json file. Again, this section contains a list of events will activate our extension whenever they occur. By default, it is set to activate when our command is run.

In theory, this event could be anything, and more specifically "*" anything. By setting the activation event to "*" this means your extension will basically be loaded when VS Code starts up. This is not required by any means, just a note.

Ok, so we've got the necessary files and we know how to debug. Now let's start building our extension. Let's say we want this extension to be able to create an html file that already has boilerplate code in it (otherwise we would have to create the file ourselves and then write the code) into our project.

Let's first update the name of our command. In the extension.js, update the name of the command from "extension.helloworld" to "extension.createBoilerplate".

Now, update the package.json accordingly.

Writing Visual Studio Code extensions is just writing Node!

Now, let's write our functionality. The first thing we'll do is require a couple of packages. We are going to use the fs (file system) and path modules.

const fs =require("fs");const path =require("path");

We also need to get the path to the current folder. It took me a bit of digging (and many console logs) to figure out but I eventually did. Inside of the command, add the following snippet.

Now we need to write to the file. We can call the writeFile function of the file system module and pass in the folder path and html content.

Notice that we use the path module to combine the folder path with the name of the file we want to create. Then inside of callback, if there is an error, we display that to the user. Otherwise, we let the user know that we created the boilerplate file successfully!

If there's something VS Code doesn't do, install an extension. If you can't find the right extension, create it yourself. The Code team has done a great job of creating tools to help make extension development pretty streamlined.

The hardest part in my mind is reading through the documentation to learn what APIs there are to use and how to use them. So, I would recommend spending some time in the docs!

Jobs!

James has a true passion for Web Development, Tooling, and Design. He follows the latest blogs, speaks at community events, and participates in Hackathons. James spent three years at Microsoft as a Technical Evangelist in New York City and Miami and is currently working at FedEx Services in Memphis as a Full-Stack Web Developer. He considers himself to be a "Social Developer" because of his love for working with people, and in his spare time, plays in weekly lunch basketball games, trains for half marathons, and solves a Rubik's cube in under two minutes.