1. 引言
GitHub Copilot 是由 GitHub 推出的一款 AI编程助手 ,基于大量 公开源代码训练 的 深度学习模型,旨在帮助开发者更快、更智能地编写代码,于2022年6月22日正式上线。
💁♂️ 所以,不是什么新东西了,深度使用了 Github Copilot + GPT4 好一阵子,感觉自己成了一个
不会写代码,只会 问问题 & 按Tab键位的废物开发🤡
以前折腾过一些 提高开发效率 的伎俩,用于 自动生成代码:
- 《🤡花3分钟,重拾开发效率神器 → Live Templates》
- 《🐶再花2分钟,试下冷门小工具 → Postfix Completion》
- 《🤖 自定义IDEA插件 → 划词翻译替换(上)》
- 《🤖 自定义IDEA插件 → 划词翻译替换(下)》
但在这些 AI编程助手 面前,真就是个 弟弟 ,这TM才叫 自动生成 啊!除 Github Copilot 外,期间还体验了几个国产平替,如 CodeGeex 、通义灵码 等,最后发现还是 Copilot 好用 😄,它的核心功能:
- 智能代码生成 :根据 当前代码的上下文 或 开发者编辑的注释 进行 推理 并生成相应的代码片段,包括但不限于 函数、变量、类、测试代码、注释以及文档字符串等。它通常能提供 恰当的补全选项 ,在大多数情况下,只需下 按Tab键 即可轻松完成代码编写。
- 代码理解与互动:Copilot会回答你提的问题,在编码过程中提供即时的帮助和指导,包括:代码解释、代码优化建议、代码Debug等。
🤔 对了,注意和微软推出的 Microsoft Copilot 区分开来:
Microsoft Copilot 是微软推出的 AI助手 ,并集成到Windows 11中,面向 更广泛的用户群体。带来的具体功能和优势包括深度的人工智能体验、跨产品的任务完成能力、智能助理对话、日常 AI 同伴的角色、语音或文本交互方式、全系统集成、深入系统集成与操作控制、打造"杀手级"应用的可能性以及提升 PC 体验的新技能。
Tips:😁 本文后续提到的 Copilot 均指 Github Copilot!!!
2. Github Copilot 不是免费的!!!
😬 那么好用的工具,自然是要💰的,采用 按月收费的订阅制 ,也可以选择年付,价格如下(单位: ❗️ 美刀):
- 个人版 : <math xmlns="http://www.w3.org/1998/Math/MathML"> 10 / 月、 10/月、 </math>10/月、100/年
- 商业版:$19/月/用户
- 企业Plus版:$39/月/用户
使用 国外信用卡 或 PayPal 作为支付方式,都没有的话,可以搞个 国外虚拟信用卡 ,网上这类教程很多,需要的自己搜下。GitHub Copilot 提供了 30天的免费试用期,到期后需要付费订阅才能继续使用。
😏 对了,如果你是 经过认证的学生/老师 或 流行开源库的贡献者 是可以免费使用的,具体可以打开 Github Copilot 官网 查看,如果显示的是 Use Github Copilot for free 而非 Start your 30-day free trial,恭喜你可白嫖一年Copilot 啦 🎉
😑 当然,官方订阅价格对于普通开发崽来说,有点小贵,x宝和x鱼上主要有这两种 "平价" 的使用方式:
- 学生包 :不稳定,存在 封号风险 !建议 开个小号,不要拿自己的主账号来冒险!有时还需要复验学生身份。
- 远程授权 :认准企业授权!有些无良商家使用基于30天免费使用授权,进行低价销售,极其不稳定。
😐 最后,还有 老板可能跑路的风险,所以买的话尽量月付的,万一真润了,损失可以少一点~
3. Github Copilot 安装
😄 假定读者已经解决了费用问题,接着说下如何在两个主流IDE上用上 Github Copilot~
3.1. VS Code
点击左侧 Extensions(扩展) ,搜索 Github Copilot,把这两个插件都装上。

Github Copilot 需要科学上网,得配下代理,点击 Settings ,搜索 proxy,填下代理地址,笔者用的Clash,默认7890端口,设置完 Ctrl+S保存下:

接着需要 Github账号授权登录 ,点下右下角的Copilot图标,会弹出授权,跟着提示走一遍就好了。授权成功 后,小图标上的斜线会没了,点击后顶部会出现这样的弹窗:

3.2. JetBrains IDEs
依次点击顶部 File → Settings → Plugins → Marketplace → 搜索 Github Copilot ,点击 Install 安装:

同样需要设置下代理,Settings这里搜 proxy ,定位到 HTTP Proxy 选项,在对应位置填写代理地址:

接着点右下角小图标,点击 Login to Github:

同样是弹窗,跟着提示走一波,授权成功,会出现这样的提示~

4. Github Copilot 使用
💁♂️ 下面是笔者的使用经验总结,想看更详细/最新的教程可移步至《GitHub Copilot 官方文档》进行学习~
4.1. 常用快捷键
- Tab → 接受建议。
- Esc → 拒绝建议。
- Alt + ] → 下一条建议 (有多条时才能切换)。
- Alt + [ → 上一条建议。
Alt + \
→ 触发建议 (Copilot没给出建议或不小心拒绝了)。- Ctrl + Enter → 单独打开一个Copilot面板,提供10条建议。
- Ctrl + Shift + C → 打开Github Copilot Chat
😐 默认快捷键可能存在冲突,比如:在我的PyCharm中,Ctrl + \ 是 展开File选项卡。点击 Edit Keyboard Shortcuts... 自定义下快捷键:

搜索框输入 Copilot 进行过滤,定位到要修改的选项,双击,点击 Add Keyboard Shortcut 添加快捷键:

然后,可以看到冲突项:

试了好几个,发现 Alt+. 没被占用,就用这个 触发建议 啦,点击 OK 进行保存。

VS Code的修改快捷键的方式类似~
4.2. 智能代码生成
😑 感觉核心就这两点:
- 回车/代码变动 后 被动 等 Copilot 给出 补全建议 ,Tab接收建议 或 Esc拒绝建议。
- 注释 里写 prompt (提示词) ,阐述 生成目标内容的逻辑与步骤,Copilot生成对应内容。
前者无🧠Tab,后者只要 prompt玩得溜,可以玩出🌸,简单演示下~
① 描述生成的函数需求

② 测试函数

③ 写注释

😂 这里缩减了Gif的帧数,实际上没有那么丝滑,回车完还是得等一下Copilot提示的~
④ 让要求更加详细具体,比如指定函数名,参数命,注释的语言等:

回车换行后,并不会触发提示建议,需要 Ctrl + Enter → 单独打开Copilot面板:

点击 Refresh ,等待Copilot生成10条建议,挑选喜欢的提示,点击 Accept solution 接受建议,也可以按需复制粘贴合适的内容:

🤏综上,生成怎么样的内容,取决于你的提示词怎么写,你可以拿Copilot来 翻译:

你问我答:

😂 甚至是 辅助文档编写,比如编写 README.md 文件:

不过markdown默认是没开启建议的,点击 Edit Setting ,找到 markdown,把value从false改为true就可以了。

4.3. 代码理解与互动
😳 两款IDE上的 Github Copilot Chat 功能有些许不同。命令 是为了在使用Copilot Chat时提供更专业或针对性的帮助与交互。非强制,比如,你用 解释下xxx 代替 /explain 也是可以的!
4.3.1. JetBrains IDEs

😄 可以把它当成 简化版的ChatGPT 来使用,不过它只会回答技术相关的问题:

在聊天框输入 / ,它可以针对你 选中的代码/当前鼠标的位置/当前的文件或项目 进行:

比如解释代码,选中解释的代码行,除了在聊天框输入 /explain 外,也可以右键依次点击 Github Copilot → Explain This:

对了,默认使用英文解释,如果需要中文,可以再 /explain 后加个 使用中文:

生成的代码,除了复制,还可以将代码直接插入到光标处~

4.3.2. VS Code

点底部Copilot的图标,选中 Github Copilot Chat ,键入 /help,发现命令多了不少:

组合命令示例:检查终端报错 → @workspace /explain #terminalLastCommand
生成代码 ,除了支持插入到 光标处 外,还可以插入到 新文件 或 终端 中。

另外,VS Code的Copilot插件还支持 行内快捷操作 :Ctrl + I → 在当前光标行唤起一个输入框,用于提问或输入命令:

如果是全局唤起的话,可以按 Ctrl + Shift + I:

5. 小结
🤔 感觉Github Copilot目前的基本玩法就这些,有遗漏的、或者什么骚操作欢迎在评论区补充,谢谢🙏~