字
字节笔记本
2026年6月21日
将所有文本文件默认用 Sublime Text 打开
API中转
¥120
任务目标
在 macOS 上把所有常见的文本 / 源代码文件类型的默认打开应用注册为 Sublime Text。
环境
- 系统:macOS (darwin 25.5.0 arm64)
- Sublime Text 安装位置:
/Applications/Sublime Text.app - Bundle Identifier:
com.sublimetext.4
最终结果
| 指标 | 数量 |
|---|---|
| 目标扩展名总数 | 107 |
| 成功绑定 Sublime | 104 |
| 未绑定(系统级强势声明) | 3(.plist→Xcode、.csv/.tsv→Numbers) |
实施步骤
1. 安装 duti
duti 是 macOS 上批量设置默认应用的命令行工具。
bash
# brew 因 untrusted tap 拦截,需绕过
HOMEBREW_NO_REQUIRE_TAP_TRUST=1 brew install duti2. 绑定已有 UTI 的 49 个扩展名
这些扩展名在系统里已有稳定 UTI,duti 可直接绑定。
bash
duti -s com.sublimetext.4 .txt all
duti -s com.sublimetext.4 .md all
# ... 其余同理(.json .yaml .py .ts .cpp .sh .sql 等)3. 绑定抽象 UTI(覆盖所有文本/源码类型)
bash
duti -s com.sublimetext.4 public.text all
duti -s com.sublimetext.4 public.plain-text all
duti -s com.sublimetext.4 public.source-code all4. 攻克 59 个「无 UTI」扩展名(关键难点)
问题
这 59 个扩展名(.go .jsx .rs .tf .vim .gitignore 等)在系统里只有动态 dyn.* UTI。LaunchServices 拒绝为动态 UTI 设置默认应用(报错 error -50):
failed to set com.sublimetext.4 as handler for dyn.ah62d4qmxhk2x435t (error -50)
解决方案:自建 UTI 声明包
构建一个携带 UTExportedTypeDeclarations 的 app bundle,为每个扩展名注册稳定的 local.sublime.* UTI(conform to public.source-code + public.plain-text)。
位置:~/.local/SublimeDefaultTypes.app(⚠️ 不要删除,否则 UTI 失效)
Info.plist 核心结构:
xml
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeIdentifier</key><string>local.sublime.go</string>
<key>UTTypeConformsTo</key>
<array>
<string>public.source-code</string>
<string>public.plain-text</string>
</array>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array><string>go</string></array>
</dict>
</dict>
<!-- 其余 58 个扩展名同结构 -->
</array>注册并绑定
bash
LSREG="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
# 注册声明包到 LaunchServices
"$LSREG" -f ~/.local/SublimeDefaultTypes.app
# 为每个 UTI 绑定 Sublime(editor role 更强势)
for e in go jsx rs tf vim gitignore ...; do
duti -s com.sublimetext.4 "local.sublime.${e}" all
duti -s com.sublimetext.4 "local.sublime.${e}" editor
done5. 从其他 IDE 手中抢回绑定
应用级的扩展名专属声明优先级高于 UTI handler。需用 editor role 显式覆盖:
| 扩展名 | 原默认应用 | 现默认应用 |
|---|---|---|
.go | GoLand | ✅ Sublime Text |
.jsx | Zed | ✅ Sublime Text |
.rs | Zed | ✅ Sublime Text |
.kt | IntelliJ IDEA | ✅ Sublime Text |
.tf / .env | Warp | ✅ Sublime Text |
.vim | TextEdit | ✅ Sublime Text |
.rst / .scss | Kiro | ✅ Sublime Text |
已绑定的扩展名清单(104 个)
text
# 纯文本 / 标记
txt text log md markdown rst
# 数据 / 配置
json json5 jsonc yaml yml toml ini cfg conf properties
# 标记语言
xml svg xsl xslt rss atom html htm xhtml
# 样式
css scss sass less styl
# JS 生态
js mjs cjs jsx ts tsx coffee vue svelte astro
# 编程语言
py rb pl pm php go rs c h cpp cc cxx hpp hxx java kt kts scala swift
# 脚本
sh bash zsh ksh fish ps1 bat cmd awk sed
# 数据查询
sql graphql gql proto thrift
# 配置 / 杂项
gitignore gitattributes gitconfig editorconfig env csv tsv diff patch
# 基础设施 / 函数式
tf tfvars hcl nim lua ml mli hs elm ex exs erl clj cljs
# 编辑器 / 构建
vim vimrc emacs dockerfile makefile cmake rake gulpfile gemfile未绑定的扩展名(3 个,建议保留)
| 扩展名 | 默认应用 | 原因 |
|---|---|---|
.plist | Xcode | 二进制 plist,Xcode 处理更合适 |
.csv | Numbers | 表格类型 |
.tsv | Numbers | 表格类型 |
⚠️ 重要提醒
- 缓存刷新:LaunchServices 绑定可能需要 注销/重启 后才完全生效。若个别文件双击仍走旧应用,在 Finder 右键「显示简介 → 打开方式 → Sublime Text → 全部更改」一次即可。
- 勿删声明包:
~/.local/SublimeDefaultTypes.app是 59 个 UTI 的来源,删除会导致绑定失效。 - 卸载方法:
bash
rm -rf ~/.local/SublimeDefaultTypes.app "$LSREG" -kill -r -domain local -domain user
涉及的核心工具
| 工具 | 用途 |
|---|---|
duti | 设置默认应用绑定(UTI / 扩展名 → app) |
lsregister | 注册 app bundle、刷新 LaunchServices 数据库 |
UTExportedTypeDeclarations | 在 app bundle 的 Info.plist 中声明自定义 UTI |
分享: