VS Code 中的 GitHub Copilot 速查表

Visual Studio Code 中的 GitHub Copilot 提供了 AI 驱动的功能,帮助您更快、更轻松地编写代码。本速查表提供了 Visual Studio Code 中 GitHub Copilot 功能的快速概览。

您可以通过聊天视图、直接在编辑器中、从集成终端以及通过VS Code用户界面中的AI增强功能访问GitHub Copilot。

Tip

如果您还没有Copilot订阅,您可以通过注册Copilot免费计划免费使用Copilot,并获得每月一定数量的完成和聊天互动限制。

团队正在不断努力改进VS Code中的Copilot并添加新功能。备忘单中标记有的项目是实验性功能。尝试它们并在我们的问题中分享您的反馈。

与GitHub Copilot聊天

使用自然语言与GitHub Copilot聊天,并获得编码任务的帮助。例如,要求Copilot解释一段代码或一个编程概念。获取更多关于使用Copilot Chat的信息。

Action Description
⌃⌘I (Windows, Linux Ctrl+Alt+I) Open the Chat view and start a chat conversation with Copilot by using natural language.
⇧⌘I (Windows Ctrl+Shift+I, Linux Ctrl+Shift+Alt+I) Open the Copilot Edits view and start a code editing session across multiple files.
⇧⌥⌘L (Windows, Linux Ctrl+Shift+Alt+L) Open Quick Chat and ask a quick question to Copilot.
⌘I (Windows, Linux Ctrl+I) Start Inline Chat to send a chat request to Copilot directly from the editor. Use natural language or use / commands to give instructions to Copilot.
Type @ in chat or select to view the list of chat participants, which are domain experts that can help you in a specific area. Extensions can also contribute additional participants.
Example: @workspace how is auth implemented?
Participant detection Copilot Chat can also automatically route your question to the appropriate participant. Get more info.
/ Invoke a slash command to prompt for commonly used actions, such as explaining a block of code, generating tests or documentation.
/explain Ask Copilot to explain a block of code or a programming concept.
Select this icon in the Chat view to access your history of chat sessions.
Enter a chat prompt by using speech (voice chat). The chat response is read out aloud.

提示

  • 使用 / 命令和 @ 参与者以获得更精确和相关的答案。
  • 要具体,保持简单,并提出后续问题以获得最佳结果。
  • 通过将文件、符号或选择附加到您的聊天提示中来提供上下文。

代码编辑会话(预览)

使用Copilot Edits启动代码编辑会话,您可以在其中快速迭代AI生成的代码编辑,这些编辑直接应用于工作区中的多个文件。

Action Description
⇧⌘I (Windows Ctrl+Shift+I, Linux Ctrl+Shift+Alt+I) Open the Copilot Edits view and start a code editing session across multiple files.
Start a new edit session.
Accept Accept all current edits.
Discard Discard all current edits.
View all edits in a multi-file diff editor.
Undo the last edit.
Redo the last edit.
Add Files... Attach files to working set.

提示

  • 将所有您想要获取编辑的文件添加到工作集中。
  • 具体且精确地描述你希望Copilot Edits所做的更改。
  • 如果你有一个较大的任务,将其分解为较小的任务并经常迭代。

从聊天生成代码

Copilot 可以根据您的聊天提示生成代码块。快速将生成的代码应用到您的项目中或插入到新文件中。例如,要求 Copilot 优化您代码中的算法。

Action Description
Smart-apply the generated code block in the active editor.
Insert the generated code block at the cursor.
Copy the generated code block to the clipboard.
Insert the generated code block in the terminal as a shell command.
Insert into New File Insert the generated code block in a new file.

提示

将上下文附加到您的提示中

当你向Copilot发送聊天提示时,你可以附加上下文以帮助Copilot更好地理解你的问题。例如,将当前编辑器选择、文件或符号添加到你的聊天提示中。获取更多关于使用Copilot的最佳实践的信息。

Action Description
(⌘/ (Windows, Linux Ctrl+/)) Open a Quick Pick to select relevant context for your chat prompt. Choose from workspace files, symbols, current editor selection or visible contents, terminal selection or last run command, or the VS Code API.
Enable/disable automatically attaching open editors as context.
#codebase Context variable: add relevant workspace content as context to your prompt.
#editor Context variable: add the visible contents of the active editor as context for your prompt.
#selection Context variable: add the current editor selection as context to your prompt.
#terminalSelection Context variable: add the current terminal selection as context to your chat prompt.
#terminalLastCommand Context variable: add the last run terminal command as context to your chat prompt.
#VSCodeAPI Context variable: add the VS Code API as context to your prompt to ask questions related to VS Code extension development.
#file Open a Quick Pick to select a file from your workspace and add it as context for your prompt.
#<filename> Type #, followed by a filename, to get filename suggestions for workspace files and attach as context.
#sym Open a Quick Pick to select a symbol from your workspace and add it as context for your prompt.
#<symbol> Type #, followed by a symbol name, to get symbol suggestions for workspace files and attach as context.
Drag & drop file Drag & drop a file or editor onto the chat to attach the file as context.
Recent files Automatically include recently opened and edited files in your chat prompt. Get more info.

提示

  • 通过使用快速选择中的右箭头键快速添加多个文件作为上下文
  • 通过选择代码或提及聊天变量明确包含相关上下文
  • 查看聊天响应中使用的引用,以确保上下文相关

编辑器中的Copilot

当你在编辑器中编码时,你可以使用Copilot在输入时生成代码补全。调用内联聊天来提问并从Copilot获取帮助,同时保持编码的流畅性。例如,要求Copilot为函数或方法生成单元测试。获取更多关于代码补全内联聊天的信息。

Action Description
Code completions Start typing in the editor and Copilot provides code suggestions that match your coding style and take your existing code into account.
Code comments Provide a code completion prompt to Copilot by writing instructions in a code comment.
Example: # write a calculator class with methods for add, subtract, and multiply. Use static methods.
⌘I (Windows, Linux Ctrl+I) Start Inline Chat to send a chat request to Copilot directly from the editor. Use natural language or use / commands to give instructions to Copilot.
Prompt from the editor Start typing natural language directly in code and Copilot detects that you're not writing code but prompting, and will automatically start Inline Chat for your prompt.
F2 Get AI-powered suggestions when renaming symbols in your code.

提示

  • 使用有意义的方法或函数名称,以便更快地获得更好的代码补全。
  • 选择一个代码块来限定你的内联聊天提示的范围,或者通过附加文件或符号来附加相关上下文。
  • 使用编辑器上下文菜单选项直接从编辑器访问常见的Copilot操作。

自定义AI代码生成

Copilot 可以生成与您的团队或项目的编码风格、工具和开发者工作流程相匹配的响应,前提是它拥有正确的上下文。您可以提供自定义指令来帮助 Copilot 适应您的偏好,这样您就不必每次发出 Copilot 请求时都提供这些详细信息。这些自定义指令会自动添加到您的请求中。获取更多关于 在 VS Code 中自定义 Copilot 的信息。

Action Description
File-based instructions (Preview) Define shared instructions for code generation in a .gitHub/copilot-instructions.md file in your workspace. These common instructions supplement your own personal code-generation instructions.
Code-review instructions (Preview) Define instructions for using Copilot to review an editor selection in settings or import from a file. You can define language-specific instructions.
Code-generation instructions Define instructions for code generation with GitHub Copilot in settings or import from a file. You can define language-specific instructions.
Test-generation instructions Define instructions for test generation with GitHub Copilot in settings or import from a file. You can define language-specific instructions.
Commit-message generation instructions Define instructions for commit message generation with GitHub Copilot in settings or import from a file. You can define language-specific instructions.

提示

  • 定义特定语言的指令,以便为每种语言生成更准确的代码。
  • 将您的指令存储在文件中,以便轻松与团队和跨项目共享。

代码审查(预览)

Copilot 可以快速审查代码块或对工作区中未提交的更改进行审查。审查反馈会以评论的形式显示在编辑器中,您可以在其中应用这些建议。

Action Description
Review and Comment (Preview) Select a block of code, and select Copilot > Review and Comment from the editor context menu for quick review pass.
Copilot Code Review Select the Copilot Code Review button in the Source Control view for a deeper review of all uncommitted changes. Join the waitlist.

生成测试

Copilot 可以为代码库中的函数和方法生成测试。获取更多关于Chat 中的斜杠命令的信息。

Action Description
/tests Generate tests for all or only the selected methods and functions in the editor. The generated tests are appended in an existing tests file or a new tests file is created.
/setupTests Get help setting up a testing framework for your code. Get recommendation for a relevant testing framework, steps to set up and configure it, and suggestions for VS Code testing extensions.
/fixTestFailure Ask Copilot for suggestions on how to fix failing tests.
Test coverage Generate tests for functions and methods that are not yet covered by tests. Get more information.

提示

  • 提供有关要使用的测试框架或库的详细信息。

生成文档

为代码库中的函数和方法生成代码文档。获取更多关于Chat中的斜杠命令的信息。

Action Description
/docs Generate documentation comments for all or only the selected methods and functions in the editor.

调试和修复问题

使用Copilot帮助修复编码问题,并在VS Code中获取配置和启动调试会话的帮助。

Action Description
/fix Ask Copilot for suggestions on how to fix a block of code or how to resolve any compiler or linting errors in your code. For example, to help fix unresolved Node.js package names.
/fixTestFailure Ask Copilot for suggestions on how to fix failing tests.
/startDebugging Generate a launch.json debug configuration file and start a debugging session from the Chat view. Get more information.
copilot-debug command Terminal command to help you debug your programs. Prefix a run command to start a debugging session for it (for example, copilot-debug python foo.py). Get more information.

提示

  • 提供关于您需要的修复类型的额外信息,例如优化内存消耗或性能。
  • 在编辑器中留意Copilot代码操作,这些操作会提示修复代码中的问题。

搭建一个新项目

Copilot 可以帮助你通过生成项目结构的脚手架来创建一个新项目,或者根据你的需求生成一个笔记本。

Action Description
/new Use the /new command in the Chat view to scaffold a new project or a new file. Use natural language to describe the type of project/file you need, and preview the scaffolded content before creating it.
Example: /new Express app using typescript and svelte
/newNotebook Use the /newNotebook command in the Chat view to generate a new Jupyter notebook based on your requirements. Use natural language to describe what the notebook should contain.
Example: /newNotebook get census data and preview key insights with Seaborn.

源代码控制和问题

Copilot 可以分析您的提交和拉取请求中的更改,并提供提交消息和拉取请求描述的建议。

Action Description
Commit Generate a commit message for the current changes in a source control commit.
Pull request Generate a pull request title and description that correspond with the changes in your pull request.
@github Use the @github participant in chat to ask about issues, pull requests, and more across your repositories. Get more information about the available GitHub skills.
Example: @github What are all of the open PRs assigned to me?, @github Show me the recent merged pr's from @dancing-mona

使用Copilot在搜索视图中获取更相关的搜索结果。

Action Description
Semantic search Include search results from Copilot in the Search view that are semantically relevant.

终端

获取有关shell命令的帮助以及如何在终端中运行命令时解决错误。

Action Description
⌘I (Windows, Linux Ctrl+I) Start Inline Chat within the terminal to use natural language to quickly get and run a shell command.
Example: how many cores on this machine?

Fix using Copilot
Select the icon on a failed shell command and get suggestions on how to resolve the error.

Explain using Copilot
Select the icon on a failed shell command and get an explanation about why the command failed.
@terminal Use the @terminal participant in the Chat view to ask questions about the integrated terminal or shell commands.
Example: @terminal list the 5 largest files in this workspace
@terminal /explain Use the /explain command in the Chat view to explain something from the terminal.
Example: @terminal /explain top shell command

Python 和 Notebook 支持

你可以使用Copilot Chat来帮助你在原生Python REPL和Jupyter笔记本中完成Python编程任务。

Action Description
Generate
⌘I (Windows, Linux Ctrl+I)
Start Inline Chat in a notebook to generate a codeblock or Markdown block.
# Attach variables from the Jupyter kernel in your chat prompt to get more relevant responses.
Native REPL + ⌘I (Windows, Linux Ctrl+I) Start Inline Chat in the Native Python REPL and run the generated commands.

VS Code 命令和 API

你可以使用Copilot来获取有关VS Code功能、设置和VS Code扩展API的帮助。获取更多关于聊天参与者的信息。

Action Description
@vscode Use the @vscode chat participant to ask questions about VS Code by using natural language.
Example: @vscode how to enable word wrapping?
@vscode /runCommand Use /runCommand with the @vscode chat participant to run a VS Code command.
@vscode /runCommand enable developer mode
@vscode /search Use /search with the @vscode chat participant to generate a VS Code search.
Example: @vscode /search python files without imports

提示

  • 如果您询问的是 VS Code 扩展 API,请使用 #vscodeAPI 聊天变量。

下一步