Ctrl+B没反应需先确认系统终端能调用python3或node等解释器;Sublime不自带运行时,必须用绝对路径配置Build System并加-u参数防缓冲,且右下角须显示对应语言标识。

Ctrl+B没反应?先确认解释器是否真能被调用
Sublime Text 不自带任何语言运行时,Ctrl+B 本质只是调用你系统里已安装的命令(如 python3、node、gcc)。如果终端里执行 python3 --version 或 node -v 报“command not found”,那 Sublime 肯定也找不到——配再漂亮的 JSON 都白搭。
常见卡点:
- Windows 用户装 Python 时没勾选 Add Python to PATH,导致
python3命令根本不存在 - macOS 上 Homebrew 安装的
python3在终端可用,但 Sublime 启动时不继承 shell 的PATH,所以依然失效 - Linux 用户可能只有
python3,而配置里写了python,拼写错一个字母就静默失败
验证方式:打开系统终端(不是 Sublime 控制台),逐个试 python3 --version、node -v、php -v 等,哪个能返回版本号,后面 build system 就必须用哪个命令名。
Python 构建系统必须加 -u,否则 input() 直接崩
Python 默认启用 stdout 缓冲,print() 输出会卡住不显示,input() 则直接抛 EOFError。这不是代码问题,是构建系统漏了 -u(unbuffered)参数。
正确配置示例(macOS/Linux):
{
"cmd": ["<code>/opt/homebrew/bin/python3</code>", "-u", "$file"],
"selector": "source.python",
"file_regex": "^[ ]*File \"(*?)\", line ([0-9]*)"
}
关键点:
-
-u必须和解释器路径在同一级数组中,且在$file之前 - 不能用
"shell_cmd": "python3 $file"—— 它不支持 stdin 交互 - Windows 用户建议额外加
"env": {"PYTHONIOENCODING": "utf-8"}防中文乱码,但-u是底线
想跑单行 JS/PHP?用 node -e 或 php -r + 选中代码
Sublime 支持对选中代码片段执行,无需保存成文件。适合快速验证表达式或小逻辑。
JS 示例(保存为 NodeEval.sublime-build):
{
"cmd": ["<code>node</code>", "-e", "console.log(eval(process.argv[1]))", "$selected_text"],
"selector": "source.js",
"working_dir": "$file_path"
}
PHP 示例:
{
"cmd": ["<code>php</code>", "-r", "$selected_text"],
"selector": "source.php"
}
注意:
- 必须先选中代码,再按
Ctrl+B,否则$selected_text为空 - 这类方式不维护变量上下文,每次都是全新执行环境
- 不支持多行交互输入,复杂逻辑仍需完整文件 + 正常构建系统
C/C++ 构建系统要分编译和运行两步,variants 是关键
纯 "cmd": ["gcc", "$file", "-o", "$file_base_name"] 只编译,不运行。想一键编译+执行,得用 variants 定义多个动作。
推荐 Windows 配置(g++.sublime-build):
{
"cmd": ["<code>g++</code>", "${file}", "-o", "${file_path}/${file_base_name}.exe"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.cpp",
"variants": [
{
"name": "Run",
"cmd": ["<code>cmd</code>", "/c", "g++", "${file}", "-o", "${file_path}/${file_base_name}.exe", "&&", "${file_path}/${file_base_name}.exe"]
}
]
}
使用时:
- 按
Ctrl+B是编译 - 按
Ctrl+Shift+B→ 选Run才真正执行 - Linux/macOS 把
cmd换成sh,去掉.exe后缀即可
最易忽略的是:C/C++ 构建系统默认不设 encoding,中文字符串输出可能乱码,建议显式加 "encoding": "utf-8"(Windows 下若仍乱码,可试 "encoding": "cp936")。

















