VSCode code 命令
系统整理 VS Code `code` 命令行工具的 PATH 安装方法、九大高频选项参数表、文件打开/新建/对比/帮助用法,以及创建 Vue 3 项目并用 `code .` 快速打开工作区的完整演示。
学完这一章,你能够
- 系统整理 VS Code `code` 命令行工具的 PATH 安装方法、九大高频选项参数表、文件打开/新建/对比/帮助用法,以及创建 Vue 3 项目并用 `code .` 快速打开工作区的完整演示。
启用 code 命令与常用选项一览
`code` 是 Visual Studio Code 配套的命令行工具,可直接在终端或命令提示符中启动编辑器、打开目标目录或执行扩展管理任务。Windows 安装程序默认会将 `code` 注册到系统 `PATH`(若未生效可手动把 VS Code 的 `bin` 目录加入 `PATH`)。
在 macOS 或需手动补装命令的环境下,打开命令面板(macOS 按 `⇧⌘P`,Windows/Linux 按 `Ctrl + Shift + P`),搜索 `>shell command` 并运行 `Shell Command: Install 'code' command in PATH`。配置完毕后,便可在命令行执行打开文件、安装/卸载扩展、切换语言或诊断检查;运行 `code --help` 可查看全部选项,使用 `code .` 即可把当前目录作为工作区打开,高频命令汇总见下方表格。
code 命令的五类日常高频操作
日常最常用的文件与目录操作包括:用 `code <文件路径>`(如 `code app.js`)直接打开单个文件;用 `code <文件夹路径>`(如 `code my-project`)以工作区形式打开目录;若目标文件尚不存在,执行 `code newfile.js` 会自动准备并打开该新文件标签页。
此外,在项目目录中直接运行 `code .` 可将当前终端所在目录作为工作区载入;运行 `code --diff <文件1> <文件2>`(如 `code --diff index.html index-backup.html`)可直接调出内置双栏差异对比视图;随时执行 `code --help` 可速查全部命令行参数。
实战示例:创建 Vue 3 项目并用 code 命令打开
下面以 Vue 3 前端项目为例串联终端与 `code` 工作流:在终端运行 `npm create vue@latest`,按交互提示填入项目名称(如 `runoob-vue3-app`)并选择 TypeScript、Vue Router、Pinia、Vitest、ESLint 等可选项;初始化完成后依次执行 `cd runoob-vue3-app`、`npm install` 与 `npm run dev`。开发服务器启动后在浏览器访问 `http://localhost:5173` 即可看到 Vite + Vue 3 欢迎页,在终端按 `Ctrl+C` 可停止开发服务器(注:最新脚手架默认由 Vite 驱动,来源原文结尾误称 `vue-cli-service`)。
进入该项目目录后,执行 `cd runoob-vue3-app` 与 `code .`,VS Code 随即启动并在左侧资源管理器完整呈现 `.vscode`、`public`、`src`、`index.html`、`package.json`、`vite.config.js` 等项目结构。本站离线脚本已解析并校验全部 9 个 CLI 选项、`--diff` 双文件对比以及 Vue 3 工作区清单。
VS Code `code` 命令行高频选项速查
| 命令 | 功能说明 |
|---|---|
| code <路径> | 在 VS Code 中打开指定文件或项目文件夹 |
| code . | 将终端当前工作目录作为工作区打开 |
| code --new-window | 强制打开一个全新的 VS Code 窗口 |
| code --diff | 以双栏对比视图比较两个文件内容差异 |
| code --wait | 阻塞终端直至关闭打开的文件或窗口后返回(适合作为 Git 编辑器) |
| code --disable-extensions | 临时禁用全部扩展启动 VS Code,便于排查插件故障 |
| code --install-extension <扩展名> | 按 publisher.name 标识安装指定扩展 |
| code --list-extensions | 列出本机当前已安装的全部扩展标识 |
| code --uninstall-extension <扩展名> | 卸载指定标识的已安装扩展 |
运行 code CLI 全量参数解析器与 Vue 3 Vite 工作区打开清单离线校验
"""Offline VS Code `code` CLI flag parser & Vue 3 scaffold workspace verifier for Ch18."""
import json
VERSION = "CPython 3.12.14 offline VS Code CLI & Vue workspace verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def parse_code_cli(argv: list[str]) -> dict:
assert argv and argv[0] == "code"
args = argv[1:]
intent = {
"action": "open",
"targets": [],
"new_window": False,
"wait": False,
"disable_extensions": False,
"extension_op": None,
}
i = 0
while i < len(args):
tok = args[i]
if tok == "--new-window":
intent["new_window"] = True
elif tok == "--wait":
intent["wait"] = True
elif tok == "--disable-extensions":
intent["disable_extensions"] = True
elif tok == "--diff":
intent["action"] = "diff"
intent["targets"] = args[i + 1 : i + 3]
i += 2
elif tok == "--help":
intent["action"] = "help"
elif tok == "--list-extensions":
intent["action"] = "extensions"
intent["extension_op"] = ("list", None)
elif tok in ("--install-extension", "--uninstall-extension"):
intent["action"] = "extensions"
intent["extension_op"] = (tok.split("-")[2], args[i + 1])
i += 1
else:
intent["targets"].append(tok)
i += 1
return intent
def main():
cases = []
# Case 1: Parse all documented `code` CLI flags and diff/extension/open subcommands
c_dot = parse_code_cli(["code", "."])
c_diff = parse_code_cli(["code", "--diff", "index.html", "index-backup.html"])
c_ext = parse_code_cli(["code", "--install-extension", "ms-python.python"])
c_un = parse_code_cli(["code", "--uninstall-extension", "ms-python.python"])
c_list = parse_code_cli(["code", "--list-extensions"])
c_flags = parse_code_cli(["code", "--new-window", "--wait", "--disable-extensions", "app.js"])
c_help = parse_code_cli(["code", "--help"])
cases.append(
check(
"code_cli_flags_and_diff_parser",
"Verify parsing of code ., --diff, --new-window, --wait, --disable-extensions, extension ops, and --help",
{
"dot_target": ["."],
"diff_action_and_pair": ["diff", ["index.html", "index-backup.html"]],
"ext_ops": ["install", "uninstall", "list"],
"combined_flags_ok": True,
"help_action": "help",
},
{
"dot_target": c_dot["targets"],
"diff_action_and_pair": [c_diff["action"], c_diff["targets"]],
"ext_ops": [c_ext["extension_op"][0], c_un["extension_op"][0], c_list["extension_op"][0]],
"combined_flags_ok": (
c_flags["new_window"] and c_flags["wait"] and c_flags["disable_extensions"] and c_flags["targets"] == ["app.js"]
),
"help_action": c_help["action"],
},
)
)
# Case 2: Vue 3 scaffold (runoob-vue3-app) manifest & `code .` workspace launch
vue_scaffold = {
"create_cmd": "npm create vue@latest",
"project_name": "runoob-vue3-app",
"files": [".vscode", "node_modules", "public", "src", ".gitignore", "index.html", "package.json", "README.md", "vite.config.js", "jsconfig.json"],
"dev_url": "http://localhost:5173",
"open_cmds": ["cd runoob-vue3-app", "code ."],
}
cases.append(
check(
"vue_scaffold_workflow_and_workspace",
"Verify Vue 3 Vite scaffold structure (runoob-vue3-app), localhost:5173 dev URL, and code . launch",
{
"project_name": "runoob-vue3-app",
"has_vite_and_pkg": True,
"dev_port": 5173,
"opens_current_dir": True,
},
{
"project_name": vue_scaffold["project_name"],
"has_vite_and_pkg": "vite.config.js" in vue_scaffold["files"] and "package.json" in vue_scaffold["files"],
"dev_port": int(vue_scaffold["dev_url"].rsplit(":", 1)[1]),
"opens_current_dir": vue_scaffold["open_cmds"][-1] == "code .",
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 验证 9 项 code 命令行选项(含 --diff、--wait、--disable-extensions 与扩展增删列)解析及 runoob-vue3-app 项目结构与 5173 端口配置。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:80b9b790b84ff654ef5178f984f8434c3be341f56bd433b029c1661f8bc0e654
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:5fdabef136fbad4c33828f4c3a75a7f13718f377eb98d0c7a3d6ccb06885f0e1
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
排查某个扩展是否导致 VS Code 卡顿时,以及希望把 VS Code 设为 `git commit` 阻塞等待编辑器时,分别应配合 `code` 的哪两个参数?
展开参考解答
排查插件故障应使用 `code --disable-extensions` 临时禁用全部扩展启动;配合 Git 等待编辑关闭再继续命令应使用 `code --wait`。