案例一

1. 設置捷徑

目標位置:"C:\Users\allen\AppData\Local\Programs\Microsoft VS Code\Code.exe" C:\workspace\python\firstproject

2. 全局環境下的settings.json

追加 python終端機設置的Profile

"terminal.integrated.profiles.windows": {
    "python": {
        "path": "C:\\Windows\\System32\\cmd.exe",
        "args": [
            "/k",
            "chcp 65001"
        ],
        "icon": "python"
    }
}

3. 專案下設置 .vscode

1. .vscode/settings.json

   {
     "terminal.integrated.defaultProfile.windows": "python",
     "terminal.integrated.enablePersistentSessions": true,
     "terminal.integrated.fontFamily": "Consolas, 'Microsoft JhengHei', monospace",
     "terminal.integrated.fontSize": 18,
     "editor.fontSize": 18,
     "editor.lineHeight": 1.8,
     // ── 編碼設定 ──────────────────────────────────
     // 專案所有檔案預設使用 UTF-8
     "files.encoding": "utf8",
     // 自動偵測 BOM(讓 VS Code 自動識別 UTF-8 BOM 檔案)
     "files.autoGuessEncoding": true,
     "markdown.preview.fontFamily": "'Microsoft JhengHei', 'PingFang TC', sans-serif"
    }

2. .vscode/tasks.json

{
  "version": "2.0.0",
  "tasks": [
     {
        "label": "Auto Terminal",
        "type": "shell",
        "command": "cd /d C:\\workspace\\python",
        "options": {
            "shell": {
                "executable": "cmd.exe",
                "args": ["/d", "/c"]
            }
        },
        "presentation": {
            "panel": "new",
            "reveal": "always",
            "focus": true,
            "close": true
        },
        "runOptions": {
            "runOn": "folderOpen"
        }
     }
  ]
}

4. 在 VS Code 中允許自動任務執行

VS Code 預設為了安全,會阻止任何在專案開啟時「自動執行」的指令。我們必須在 IDE 中跟它說「我是信任這個專案的,請幫我執行」:

  1. 用你的捷徑開啟 firstproject 專案。
  2. 點擊鍵盤上的 F1 鍵(或 Ctrl + Shift + P)開啟上方的命令面板。
  3. 在輸入框中輸入並選擇:工作: 管理自動工作
  4. 點擊 允許自動工作

5. 雲端連結

mklink /J "%UserProfile%\Desktop\VSCode IDE" "%UserProfile%\Google 雲端硬碟檔案串流\我的雲端硬碟\shortcut\vscode"

案例二

虛擬環境 dvds

1. 全域設定:User settings.json

路徑: %APPDATA%\Code\User\settings.json

2. 專案區域設定:.vscode/settings.json

路徑: [專案資料夾]/.vscode/settings.json

3. 專案自動啟動任務:.vscode/tasks.json

路徑: [專案資料夾]/.vscode/tasks.json

💡 運作原理小結

  1. 全域預設改用 Command Prompt,讓 Continue / AI 套件在啟動或背景檢測時只會呼叫 cmd.exe,不再召喚 PowerShell

  2. 當開啟特定專案資料夾時,專案內的 .vscode/settings.json 會覆蓋預設 Profile,將新建的終端機指定為帶有 Python 虛擬環境的 dvds

  3. .vscode/tasks.jsonfolderOpen 時觸發,順暢開啟單一且正確的 dvds 終端機面板!


相關主題與延伸閱讀