背景

在 macOS 中安装多个开发 IDE 后,经常会遇到文件关联混乱的问题:

例如:

  • .py 双击打开 PyCharm
  • .vue 双击打开 WebStorm
  • .md 双击打开 Cursor
  • 项目目录双击直接启动 JetBrains IDE

这会导致:

  • Finder 文件图标混乱
  • 双击行为不可控
  • 不符合统一开发工作流
  • 主要是本人强迫症

当前目标:

VS Code 作为唯一默认编辑器。

PyCharm、WebStorm、Cursor 等 IDE 只作为特殊场景工具,需要时手动启动。

最终效果:

文件
 |
 +-- Python       → VS Code
 +-- Vue          → VS Code
 +-- TypeScript   → VS Code
 +-- Markdown     → VS Code
 +-- JSON         → VS Code
 +-- YAML         → VS Code
 +-- TOML         → VS Code
 +-- Log          → VS Code
 +-- Config       → VS Code


PyCharm/WebStorm/Cursor

        ↓

只有主动点击 App 才启动

MacOS 文件关联机制

macOS 使用 LaunchServices 管理:

  • 文件扩展名
  • 默认打开程序
  • Finder 图标

例如:

.py

LaunchServices

↓

PyCharm.app

所以:

双击:

main.py

会启动:

PyCharm

修改文件关联的常见方式:

右键文件
↓
显示简介
↓
打开方式
↓
全部更改

缺点:

  • 需要一个个扩展名修改
  • 文件类型多时非常麻烦

duti 管理文件关联

什么是 duti

duti 是 macOS 的命令行文件关联管理工具。

作用类似:

Finder
右键
显示简介
打开方式

但是可以批量处理。

安装:

brew install duti

查看当前关联:

duti -x py

例如:

PyCharm.app

表示:

Python 文件由 PyCharm 打开。


VS Code Bundle ID

查看:

osascript -e 'id of app "Visual Studio Code"'

正常:

com.microsoft.VSCode

统一设置 VS Code 默认打开

覆盖常见开发文件:

duti -s com.microsoft.VSCode py all
duti -s com.microsoft.VSCode js all
duti -s com.microsoft.VSCode ts all
duti -s com.microsoft.VSCode vue all

支持的文件类型

编程语言

py
js
jsx
mjs
cjs
ts
tsx
vue
java
go
rs
c
cpp
h
hpp
cs
swift
dart
php
rb
lua

Web 文件

html
css
scss
sass
less

配置文件

json
jsonc
jsonl
yaml
yml
toml
ini
conf
cfg
env
properties

DevOps

dockerfile
tf
hcl
nginx

Shell

sh
bash
zsh
fish

文档

md
markdown
txt
rst

数据

csv
tsv
xml
sql
graphql
gql

日志

log

移除 JetBrains 与 Cursor 注册

除了修改默认打开程序,还需要移除 IDE 在 LaunchServices 中的注册。

包括:

PyCharm
WebStorm
IntelliJ IDEA
GoLand
Rider
CLion
Cursor

注销应用注册

LaunchServices 工具:

LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"

注销:

$LSREGISTER -u /Applications/PyCharm.app
 
$LSREGISTER -u /Applications/WebStorm.app
 
$LSREGISTER -u /Applications/Cursor.app

完整自动化脚本

创建:

reset-vscode-default.sh

内容:

#!/bin/bash
 
set -e
 
echo "Reset macOS editor association to VS Code"
 
 
LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
 
VSCODE="com.microsoft.VSCode"
 
 
EDITOR_APPS=(
 
"/Applications/PyCharm.app"
 
"/Applications/WebStorm.app"
 
"/Applications/IntelliJ IDEA.app"
 
"/Applications/GoLand.app"
 
"/Applications/Rider.app"
 
"/Applications/CLion.app"
 
"/Applications/Cursor.app"
 
)
 
 
echo "Remove IDE registrations"
 
 
for app in "${EDITOR_APPS[@]}"
do
 
if [ -d "$app" ]; then
 
echo "Remove $app"
 
$LSREGISTER -u "$app" || true
 
fi
 
done
 
 
 
echo "Set VS Code default"
 
 
EXTENSIONS=(
 
py
pyw
 
js
jsx
mjs
cjs
 
ts
tsx
 
vue
 
html
htm
 
css
scss
sass
less
 
json
jsonc
jsonl
 
yaml
yml
 
toml
 
ini
conf
cfg
 
env
 
sh
bash
zsh
 
md
markdown
 
txt
 
csv
tsv
 
xml
 
sql
 
graphql
gql
 
log
 
dockerfile
 
tf
hcl
 
gitignore
gitattributes
 
)
 
 
for ext in "${EXTENSIONS[@]}"
do
 
duti -s "$VSCODE" "$ext" all 2>/dev/null || true
 
done
 
 
 
echo "Folder open with Finder"
 
 
duti -s com.apple.finder public.folder all || true
 
 
 
echo "Refresh LaunchServices"
 
 
$LSREGISTER \
-kill \
-r \
-domain local \
-domain system \
-domain user
 
 
 
killall Finder || true
 
 
echo "Done"
 

执行:

chmod +x reset-vscode-default.sh
 
./reset-vscode-default.sh

Finder 图标刷新问题

注意:

文件关联和 Finder 图标是两个系统。

可能出现:

双击文件

↓

VS Code 打开 ✅


但是:

Finder 图标

↓

仍显示 PyCharm ❌

原因:

macOS 图标缓存没有刷新。


清理图标缓存

执行:

rm -rf ~/Library/Caches/com.apple.iconservices.store

然后:

killall iconservicesagent
 
killall Finder
 
killall Dock

重新注册 VS Code:

LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"
 
 
$LSREGISTER \
-f \
"/Applications/Visual Studio Code.app"

验证配置

检查:

duti -x py
 
duti -x md
 
duti -x json
 
duti -x yaml
 
duti -x toml

正确结果:

Visual Studio Code.app

推荐开发环境最终状态

个人 AI Native 开发环境:

macOS

│
├── VS Code
│
│   ├── Python
│   ├── Vue
│   ├── LLMOps
│   ├── Docker
│   ├── Config
│   └── Markdown
│
├── Obsidian
│
│   └── Knowledge Management
│
├── Claude CLI
│
├── Codex CLI
│
├── Cursor
│
│   └── 手动启动
│
└── JetBrains IDE

    └── 特殊场景手动启动

总结

最佳实践:

  • 一个主编辑器管理所有文件
  • 其他 IDE 不参与系统文件关联
  • 使用命令行工具管理 macOS LaunchServices
  • 避免 IDE 安装时自动注册文件类型

对于现代 AI 辅助开发流程:

VS Code
+
Claude
+
Codex
+
Terminal
+
Obsidian

比多个 IDE 抢占文件关联更加稳定。