Your First Extension
This tutorial walks you through creating a simple VS Code extension that displays a “Hello World” message. You’ll learn the fundamental concepts of extension development and the basic structure of an extension.What You’ll Build
You’ll create an extension that:- Registers a command called “Hello World”
- Shows an information message when the command is executed
- Can be invoked from the Command Palette
Prerequisites
Before you begin, make sure you have:
- Node.js and npm installed
- Visual Studio Code installed
- Basic knowledge of TypeScript or JavaScript
Create Your Extension
Set up the project structure
Create a new directory for your extension and initialize it:Install the VS Code types as a dev dependency:
Create the package.json manifest
Create or update your
package.json file with the extension metadata:package.json
Create the TypeScript configuration
Create a
tsconfig.json file to configure TypeScript:tsconfig.json
Create the extension entry point
Create a
src/extension.ts file with your extension code:src/extension.ts
The
activate function is the entry point of your extension. It’s called when your extension is activated (based on activationEvents).Compile the extension
Compile your TypeScript code to JavaScript:This creates the compiled JavaScript in the
out directory.Test the extension
Press
F5 in VS Code to open a new Extension Development Host window with your extension loaded.In the new window:- Press
Cmd+Shift+P(Mac) orCtrl+Shift+P(Windows/Linux) to open the Command Palette - Type “Hello World: Say Hello” and select the command
- You should see an information message: “Hello World from VS Code!”
Understanding the Code
Let’s break down the key components:The ExtensionContext
ExtensionContext is passed to your activate function and provides:
subscriptions- An array where you add disposables for cleanupextensionPath- The absolute file path of your extension directoryglobalStateandworkspaceState- Storage APIs for persisting data
Registering Commands
commands.registerCommand function:
- Takes a command identifier (must match
package.json) - Takes a callback function to execute when the command is invoked
- Returns a
Disposableto clean up the command registration
Showing Messages
window namespace provides several message functions:
Enhancing Your Extension
Now that you have a basic extension working, try adding more features:Add a Second Command
Update yourpackage.json to add another command:
package.json
src/extension.ts
Access the Active Editor
Get information about the currently active text editor:Listen to Events
React to changes in the editor:Debugging Your Extension
To debug your extension:- Set breakpoints in your TypeScript code by clicking in the gutter
- Press
F5to launch the Extension Development Host - Trigger your command in the new window
- The debugger will pause at your breakpoints
Next Steps
Extension Anatomy
Learn about the structure and lifecycle of extensions
API Overview
Explore the full VS Code Extension API