自定义编译系统可通过配置 .sublime-build 文件实现,支持运行 Python、Node.js、C++ 等代码;关键字段包括 cmd、shell_cmd、selector、working_dir 和 variants;示例包含带中文支持的 Python 运行、g++ 编译 C++、Node.js 执行及多变体构建;保存至 User 目录后在 Build System 中选择即可使用。

在 Sublime Text 中,自定义编译系统(Build System)可以让你运行外部程序或脚本,比如执行 Python、Node.js、Go 等语言的代码,甚至运行 shell 命令或构建任务。通过配置 .sublime-build 文件,你可以灵活控制构建行为。
Sublime 的编译系统是基于 JSON 格式的配置文件,保存为 .sublime-build 文件。你可以通过以下步骤创建:
Tools → Build System → New Build System...此时会打开一个模板文件,示例如下:
{ "cmd": ["python", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python", "shell": false, "working_dir": "$file_path" }
理解每个配置项的作用,有助于你按需定制:
下面是一些常见场景的配置例子:
1. 运行 Python 脚本(带中文支持)
{ "cmd": ["python", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python", "encoding": "utf-8", "env": {"PYTHONIOENCODING": "utf-8"} }
2. 编译并运行 C++ 文件(g++)
{ "cmd": ["g++", "$file_name", "-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k", "${file_base_name}.exe"], "shell": true, "working_dir": "$file_path", "selector": "source.c, source.cpp" }
3. Node.js 脚本运行
{ "cmd": ["node", "$file"], "selector": "source.js", "shell": false }
4. 添加构建变体(如调试模式)
{ "cmd": ["python", "-u", "$file"], "selector": "source.python", "variants": [ { "name": "Run in Terminal", "shell_cmd": "start cmd /k python \"$file\"" }, { "name": "Debug", "cmd": ["python", "-u", "$file", "--debug"] } ] }
将配置文件保存到默认路径(Preferences → Browse Packages → User),文件名为如 MyPython.sublime-build。保存后,在菜单栏选择:
Tools → Build System → MyPython然后按下 Ctrl+B 即可运行构建任务。
如果想临时选择,也可以使用 Command Palette(Ctrl+Shift+P),输入 “Build With”,选择对应的系统或变体。
基本上就这些。配置灵活,关键是根据实际执行命令来调整 cmd 和路径变量。以上就是sublime怎么自定义编译系统(build system)_sublime自定义构建任务与命令设置的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号