VS Code GitHub Copilot | Tutorial
\nGitHub Copilot is an AI coding assistant jointly developed by GitHub and OpenAI. It can automatically complete code, generate functions, optimize logic, and even write entire code modules based on context, helping developers code more efficiently.
\nGitHub Copilot integrates into various IDEs such as VS Code, Visual Studio, and JetBrains, making you feel like you have an experienced partner assisting you while you code.
\n\n
Core Features
\nAutomatic Code Completion
\nGitHub Copilot can provide real-time code suggestions based on your code context and comments. It can:
\n- \n
- Complete entire lines of code \n
- Generate complete functions \n
- Generate code based on descriptive comments \n
Code Explanation
\nCopilot can help you understand unfamiliar code:
\n- \n
- Explain the functionality of complex code snippets \n
- Provide code optimization suggestions \n
- Identify potential issues \n
Multi-language Support
\nSupports almost all mainstream programming languages, including:
\n- \n
- JavaScript/TypeScript \n
- Python \n
- Java \n
- C++ \n
- Go \n
- Ruby, etc. \n
\n
Installation and Setup
\nInstallation Steps
\n- \n
- Prerequisites\n
- \n
- VS Code must be correctly installed, version
1.57or higher is recommended. \n - A GitHub account, and you need to enable a GitHub Copilot subscription (Personal, Team, or Enterprise editions are all supported). \n
\n - VS Code must be correctly installed, version
- Install Extension\n
- \n
- Open VS Code and go to the Extensions Marketplace. \n
- Search for
GitHub Copilot. \n - Click
Installto install. \n
\n
After installation, you can click the small icon in the top right corner to open the Copilot chat window:
\nYou can also click the icon in the bottom right corner:
\n- \n
- Log in to GitHub Account\n
- \n
- After installation, VS Code will prompt you to log in to your GitHub account. \n
- Click
Sign in to GitHub. The browser will redirect to the GitHub authorization page. After authorizing, it will automatically return to VS Code. \n
\n
- \n
- Enable GitHub Copilot\n
- \n
- In VS Code, press
Ctrl + Shift + Pand typeCopilot: Enable. \n - Once confirmed, you can start using it. \n
\n - In VS Code, press
After installation, open VS Code settings and search for Copilot. Here you can adjust settings in VS Code:
\n- \n
- Suggestion trigger method (automatic or manual) \n
- Suggestion display position \n
- Language preference settings \n
\n
Usage Tips
\nSmart Completion
\nWhen you start typing code, GitHub Copilot will automatically provide suggestions, for example:
\nfunction fetchData(url) { // Copilot will automatically provide an implementation based on the fetch API}\n\nWe can press the Tab key to accept the suggestion.
\n- \n
- If there are multiple suggestions, press
Ctrl + ]orCtrl + [to switch. \n - If you don't want to use the suggestion, press
Escto cancel. \n
Generate Code from Comments
\nSimply write a comment, and Copilot will implement it for you:
\n# Implement a quicksort algorithm\n\nExample
\n# Implement a quicksort algorithm\n\ndef quick_sort(arr):\n\nif len(arr)<=1:\n\nreturn arr\n\n pivot = arr[len(arr) // 2]\n\n left =\n\nreturn quick_sort(left) + middle + quick_sort(right)\n\nGenerate Test Cases
\nYou can even write comments directly to let Copilot generate tests for you:
\n// is quicksort Generate Unit Tests for Functions\n\nAdditionally, you can right-click in the editor, select Copilot, and bring up the desired feature:
\nShortcut Key Reference:
\n| Shortcut Key | \nFunction Description | \n
|---|---|
Tab | \nAccept suggestion | \n
Esc | \nIgnore suggestion | \n
Ctrl + ] | \nView next suggestion | \n
Ctrl + [ | \nView previous suggestion | \n
Ctrl + Enter | \nOpen Copilot panel | \n
Ctrl + Shift + P | \nEnable/Disable Copilot | \n
\n
Using Chat Features in VS Code
\nTo use the chat feature in Visual Studio Code, you can ask questions about your codebase in natural language, or perform multi-file editing within your project. The chat feature can run in different modes according to your needs, satisfying everything from asking questions and multi-file editing to initiating agent-based coding workflows.
\nYou can use VS Code's chat feature in the following scenarios:
\n- \n
- Understanding Code: For example, "Explain how this authentication middleware works?" \n
- Debugging Issues: For example, "Why am I getting a null reference in this loop?" \n
- Getting Code Suggestions: For example, "Show me how to implement a binary search tree in Python?" \n
- Optimizing Performance: For example, "Help me improve the efficiency of this database query?" \n
- Learning Best Practices: For example, "What are the recommended methods for handling errors in asynchronous functions?" \n
- Getting VS Code Tips: For example, "How do I customize keyboard shortcuts?" \n
Tip: If you don't have a Copilot subscription yet, you can use Copilot for free by registering for the Copilot Free plan, which offers limited completions and chat interactions per month.
\nChat Modes
\nDepending on your specific needs, you can choose different chat modes:
\n| Mode | \nDescription | \nScenario | \n
|---|---|---|
| Ask | \nAsk questions about the codebase or technical concepts. | \nAvailable in Stable or Preview. Understand how code snippets work, brainstorm software design ideas, or explore new technologies. | \n
| Edit | \nPerform multi-file edits within the codebase. | \nAvailable in Stable or Preview. Apply code edits directly in your project to implement new features, fix bugs, or refactor code. | \n
| Agent | \nInitiate an agent-based coding workflow. | \nAvailable in Stable or Preview. Autonomously implement new features or advanced project requirements with minimal guidance, call tools to execute specialized tasks, and iterate to resolve issues when they arise. | \n
You can switch between different chat modes by using the mode dropdown menu in the chat view.
\n
YouTip