Create a VS Code Extension to Pass Arguments to the Terminal

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

Rather than taking the time to open the terminal to type out a command, let's create a vs code extension that will do that for us!

Instructor: [0:00] Rather than open the terminal, let's open a visual studio command. I have one called egghead io quick lesson. Hit Enter. Type a lesson name, like, "Create a VS Code Extension." Hit Enter. Then that will pop open a terminal for me and run my lesson command with that title as the input.

[0:20] Before starting a VS Code extension, I strongly recommend the samples that Microsoft put up online, because to build mine, all I had to do was take the quick input example, which is in here. I cloned this, and essentially deleted a couple things, which looks like this, where the flow is the package JSON has the commands that you register. I gave mine egghead io.lesson.

[0:43] The extension has a place where you push the commands that you register. This text matches up with the text in the package JSON. Then, whenever that command is triggered, I'm telling it to show input box for another file. This could be in the same file, but I put it in the basic input file.

[1:00] The flow is window.showInputBox, which is asking you to type a lesson name. When you hit enter, since this is a promise, it gives you back a lesson name. Then I can pass that lesson name down to a terminal. I create a new terminal, tell it to open up.

[1:14] This sets up an event to clear out the terminal once it's done. I set up my command, which is +lesson and then the lesson name. Then you can send that text to the terminal. Once that's done, you have to run the VSCE package command. It'll bundle it up into a VSIX file, which you can then install with a install from VSIX command, and you can search and pick the file.

[1:40] You can clone this repo yourself if you want to customize the basic input to do a different terminal command because, right now, it's hard-coded to lesson. I prefer to hard-code that to this extension, rather than trying to set up a configuration system.