Exploring Github Copilot
GitHub Copilot is an AI pair programmer that provides intelligent suggestions as you work. It can suggest single lines of code, entire functions, documentation, and even prose based on the context of your file and your comments. This tutorial will guide you through installing, authenticating, and using its core features in VSCode for both coding and writing. This post will explore Github Copilot's capabilities, focusing on practical examples as well as its use as a writing assistant for documentation and blog posts. !!! note Answers are non-deterministic Copilot, and all other AI tools I'm aware of, generate non-deterministic outputs. This means that if you follow the same steps multiple times, you may get different suggestions each time. The examples shown here are representative of typical outputs but may not match exactly what you see. !!! ## Prerequisites Before you start, you will need: - **A GitHub Account**: Copilot is a GitHub service. - **A GitHub Copilot Subscription**: Copilot is a paid service. GitHub offers a free trial for new users. You must sign up for a plan (either personal or business) on the GitHub website before the extension will work. - **Visual Studio Code**: Download and install it from the official VSCode website. ## Step 1: Install the GitHub Copilot Extension First, you need to add the Copilot extension to your VSCode editor. 1. Open VSCode. 2. Click the Extensions icon in the Activity Bar on the left side (it looks like four squares). 3. In the search bar, type GitHub Copilot. 4. You will see several extensions. Select the main one named GitHub Copilot (published by GitHub). 5. Click the Install button. This will often install GitHub Copilot Chat as well, which is highly recommended for the features we'll cover. ## Step 2: Authenticate Your Account After installation, you must link VSCode to your GitHub account to verify your subscription. 1. After installation, a small pop-up will appear in the bottom-right corner prompting you to sign in. 2. Click Sign in to GitHub. 3. VSCode will ask for permission to sign in. Click Allow. 4. Your web browser will open, asking you to authorize VSCode to access your GitHub account. 5. Enter the device code displayed in VSCode into your browser and authorize the request. 6. Once authorized, you can close the browser tab. VSCode is now connected to your Copilot subscription. You can also check your status by clicking the Copilot icon in the VSCode status bar (bottom-right). ## Step 3: Using Core Copilot Features Copilot has two main ways of helping you: inline suggestions and the chat interface. ### 1. Inline Suggestions This is the classic Copilot feature. As you type, Copilot will automatically suggest text, which appears as "ghost text" (faded text) right in your editor. This works for code and for natural language in text files (like Markdown). - To Accept: Press the Tab key. - To Reject: Press the Esc key. - To See Next Suggestion: Press Alt + `]` (or Option + `]` on Mac). - To See Previous Suggestion: Press Alt + `[` (or Option + `[` on Mac). Example: If you type `function slugify(text) {`, Copilot will immediately suggest the entire function body. ### 2. Comment to Code Generation This is one of Copilot's most powerful features. You write a clear, descriptive comment, and Copilot will write the code for you. Simply write a comment, press Enter, and wait a moment. Copilot will suggest the code to implement your comment. ### 3. Requirements to Code Generation Similar to comment to code but more detailed. You can write a list of requirements, and Copilot will generate the corresponding code. Because you've only written requirements and have not started any code yet, results may vary more widely and you'll have to be more careful when verifying the output. ### 4. Using the Copilot Chat The Copilot Chat view (accessible from the Activity Bar) is an even more powerful way to interact with the AI. You can ask questions, ask it to fix code, or generate new files. - Ask for Code: You can ask it to generate code for a specific task. - **Prompt**: "Write a function that debounces a function call." - Explain Code: Select a block of code in your editor, open the chat, and ask: - **Prompt**: /explain this selected code - Fix Code: Select code that has a bug and ask: - **Prompt**: /fix this bug - Generate Tests: Select a function and ask: - **Prompt**: /tests generate unit tests for this function ## Step 4: Practical Code Examples Let's see how Copilot handles both JavaScript and TypeScript for the same task: fetching data from an API. ### TypeScript Example Create a file named api.ts. Copilot is great at inferring or generating TypeScript interfaces. ```typescript // File: api.ts // 1. Start by typing this comment: // Create a TypeScript interface for a User with id, name, and email // 2. Press Enter. Copilot will suggest: interface User { id: number; name: string; email: string; } // 3. Now, type this comment: // Create an async function to fetch an array of Users from JSONPlaceholder // 4. Press Enter. Copilot will suggest: async function fetchUsers(): Promise