AI 程式設計教程中文版
官方教程中文版入口與使用場景

VS Code 中的 Copilot

說明 VS Code Copilot 的 agents、Plan、Sessions、inline suggestions、inline chat、smart actions、customization 和安全邊界。

VS Code 是 Copilot 能力最集中的入口之一。VS Code 官方文件把它描述為把 AI agents 帶進編輯器:你描述要構建什麼,agent 自己規劃方法、寫程式碼、驗證結果;更小的改動則用 inline suggestions 和 chat 精確控制——一句話:大改用 agent,小改用 inline。

這意味著 VS Code 裡的 Copilot 不是單一聊天框,而是一組工作面:agent session、Plan、inline suggestions、inline chat、smart actions、custom instructions、agent skills、custom agents、MCP、hooks。

閱讀目標:讀完本章,你應該能在 VS Code 中選擇“Agent、Plan、inline suggestion、inline chat、smart action”中的合適入口,並知道如何驗收。

1. VS Code 的能力地圖

  • Agents:自主完成 coding task,拆步驟、改檔案、執行命令並自我修正;適合多步驟功能、bug 修復、遷移和測試驗證。
  • Plan agent:先分析程式碼庫、提出計劃和澄清問題,不直接寫程式碼;適合商業專案、大改動和需求不清的任務。
  • Sessions view:管理多個本地、後臺、雲端或第三方 agent sessions;適合並行任務、暫停恢復和交接。
  • Inline suggestions:你打字時給程式碼建議,從單行到函式;適合區域性補全和低風險小改。
  • Inline chatCmd/Ctrl+I 在編輯器裡發起區域性改寫;適合小範圍重構、解釋和快速修復。
  • Smart actions:預置常見動作,例如 commit message、rename、fix errors、semantic search;適合高頻小任務。
  • Customization:instructions、skills、custom agents、MCP 和 hooks;適合團隊規範、工具擴充套件和自動化策略。
flowchart TD
    Work["VS Code 中的任務"] --> Small{"區域性還是多步驟?"}
    Small -->|區域性補全| Inline["Inline suggestions"]
    Small -->|區域性改寫| InlineChat["Inline chat"]
    Small -->|多步驟| Plan{"需要先審計劃?"}
    Plan -->|是| Planner["Plan agent"]
    Plan -->|否| Agent["Agent session"]
    Agent --> Verify["review files / commands / tests"]
    Planner --> Agent
    Work --> Custom["instructions / skills / MCP / hooks"]
    Custom --> Agent

    style Planner fill:#dbeafe,stroke:#2563eb,stroke-width:2px
    style Verify fill:#dcfce7,stroke:#16a34a,stroke-width:2px

2. 第一次設定

VS Code 官方頁給出的入門步驟是:在 Status Bar 的 Copilot 圖示上選擇 Set up Copilot,選擇登入方式並按提示操作。如果還沒有 Copilot subscription,會註冊到 Copilot Free plan。

第一輪不要直接讓 agent 建立完整專案。更穩的順序:

  1. 開啟一個低風險儲存庫。
  2. 確認 Copilot 登入狀態。
  3. 開啟 Chat view。
  4. 先用 Ask/Chat 解釋當前檔案。
  5. 再用 inline suggestion 補一個小函式或測試。
  6. 最後嘗試一個受控 Agent session。

3. Agent session 怎麼用

VS Code 官方頁說明,agent 會處理從單檔案改動到完整 feature PR 的 coding task。它可以構建功能、除錯 failing tests、重構/遷移程式碼庫、透過 integrated browser 測試 web app,也可以透過 cloud agent 協作生成 PR。

但 agent 能做,不代表第一次就要放開。推薦 prompt:

用 agent 修复这个 failing test。
边界:
1. 先列计划,不要马上改文件
2. 只改 src/auth 和 tests/auth
3. 需要运行命令前先说明命令和原因
4. 完成后给出 diff 摘要和测试结果

驗收:

  • Sessions view 裡能看到當前任務狀態。
  • Working set 只包含預期檔案。
  • Terminal command 有人工確認。
  • 測試、build 或瀏覽器驗證真實執行。
  • 需要交給 Cloud Agent 或 PR 時,保留可 review 的分支。

4. Plan before you build

官方 VS Code 文件明確寫到:Plan agent 會在寫程式碼前分析程式碼庫、提出澄清問題併產出 step-by-step plan;計劃正確後,可以交給 implementation agent 本地、後臺或雲端執行。

適合使用 Plan:

  • 需求不清。
  • 涉及多目錄、多模組或遷移。
  • 影響鑑權、支付、資料模型、部署。
  • 需要團隊 review 方案。
  • 你不確定測試入口和回復方式。
深讀:為什麼 VS Code 的 Plan 不等於普通聊天計劃

普通聊天計劃只是模型給出的文字建議。VS Code 的 Plan agent 處在編輯器和程式碼庫上下文裡,會圍繞任務研究程式碼、提出開放問題、拆實施步驟,並能把計劃交給後續 agent 執行。它仍然需要人工審查,但比“請給我一個方案”的普通對話更適合真實工程任務。

5. 小改動用 inline suggestions 和 inline chat

官方 VS Code 文件把 “AI assistance as you type” 單獨列出來,因為很多工不需要 agent:

  • Inline suggestions:從單行補全到函式實現,適合你已經知道要寫什麼。
  • Next edit suggestions:根據當前編輯預測下一處邏輯改動。
  • Inline chat:Cmd+I / Ctrl+I 在編輯器中開啟 prompt,適合區域性解釋、重構或修復。
  • Smart actions:常見 AI 動作,例如生成 commit message、重新命名、修錯誤、語義搜尋。

判斷標準很簡單:如果你能明確指出“就改這幾行”,不要啟動大 agent;如果任務需要找檔案、跑命令、迭代修復,再用 Agent。

6. Customization 是長期質量入口

VS Code 官方文件列出幾類 customization:

  • Custom instructions:專案級編碼約定,讓 AI 輸出匹配程式碼庫風格。
  • Agent skills:跨 VS Code、Copilot CLI 和 Cloud Agent 複用專門能力。
  • Custom agents:建立特定角色 agent,例如 code reviewer、documentation writer。
  • MCP servers:透過 MCP server 或 Marketplace extension 擴充套件工具。
  • Hooks:在特定事件執行命令,用於自動化和策略 enforcement。

這些能力是商業級團隊落地 Copilot 的關鍵。沒有 customization,agent 每次都需要重新猜專案約定;有了 customization,也要定期清理過期規則,避免把舊上下文注入所有任務。

7. 組織策略和價格狀態

VS Code 官方頁提示:你的組織可能停用了 VS Code 中的 agents,需要聯絡管理員開啟。頁面還在 2026-05-06 核驗時提示:從 2026-04-20 起,Copilot Pro、Copilot Pro+ 和 student plans 新註冊臨時暫停,並且 weekly usage limits 正在收緊。

因此:

  • 團隊教程不要寫死某個 plan 永久可註冊。
  • Agent、模型、MCP、CLI、Cloud Agent 是否可用,要看管理員策略。
  • 複雜任務啟動前要知道是否會消耗 premium requests。

本章自檢

完成本章後,用這 4 個問題檢查:

  1. 當前任務應該用 inline suggestion、inline chat、Plan 還是 Agent?
  2. 如果使用 Agent,是否已經限定檔案範圍、命令許可權和驗證命令?
  3. 專案是否有 custom instructions、skills、MCP 或 hooks 需要注入?
  4. 組織是否允許 VS Code agents、模型切換和相關功能?

透過標準:你能在 VS Code 中讓 Copilot 先規劃、再受控修改、再用 diff/test/browser/PR 驗收。

官方來源

接下來去哪

本頁目錄