我的 macOS 配置

这篇文章记录下,一个新的 macOS 需要做的配置,和安装的软件。

系统设置

# 修改 accent color
defaults write -g AppleAccentColor -int 6

# 启用系统 UI 按键控制
defaults write -g AppleKeyboardUIMode -int 3

# 系统软件使用 Tab,而非独立 Window
defaults write -g AppleWindowTabbingMode -string always

# 禁止“Are you sure you want to open this application?”提示
defaults write com.apple.LaunchServices LSQuarantine -bool false

# 开启“Close windows when quitting an app”
defaults write -g NSQuitAlwaysKeepsWindows -bool false

# 禁用 crash reporter
defaults write com.apple.CrashReporter DialogType -string none

# 进入睡眠或屏保,立即密码锁定
# defaults write com.apple.screensaver askForPassword -bool true
# defaults write com.apple.screensaver askForPasswordDelay -bool false

# 禁止磁盘映像验证
defaults write com.apple.frameworks.diskimages skip-verify -bool true
defaults write com.apple.frameworks.diskimages skip-verify-locked -bool true
defaults write com.apple.frameworks.diskimages skip-verify-remote -bool true

# 不在 Desktop 显示外部磁盘、可移动介质
defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool false
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool false

########### Mouse、Keyboard、Touchbar

# 支持按住重复,并调整重复频率
defaults write -g ApplePressAndHoldEnabled -bool false
defaults write -g InitialKeyRepeat -int 13  # normal minimum is 15 (225 ms)
defaults write -g KeyRepeat -int 1          # normal minimum is 2 (30 ms)

# 加大光标大小,加快光标移动速度
defaults write -g com.apple.mouse.scaling -float 3.0
defaults write com.apple.universalaccess mouseDriverCursorSize -float 1.5

# 禁止鼠标自然模式滚动,Magic Mouse 时不用改
defaults write -g com.apple.swipescrolldirection -bool false

# 禁用输入时的自动 quotes、dashes 转换
defaults write -g NSAutomaticQuoteSubstitutionEnabled -bool false
defaults write -g NSAutomaticDashSubstitutionEnabled -bool false

# 禁止在 double space 时添加句点
defaults write -g NSAutomaticPeriodSubstitutionEnabled -bool false

# 禁止输入时自动将 word 首字母大写
defaults write -g NSAutomaticCapitalizationEnabled -bool false

# Touchbar 使用轻触代替按下
defaults write com.apple.AppleMultitouchTrackpad Clicking -bool false
defaults write com.apple.driver.AppleBluetoothMultitouch.trackpad Clicking -bool false

########### Dock、Menubar

# 自动隐藏 Dock、Menubar,充分利用空间
defaults write com.apple.dock autohide -bool true
defaults write -g _HIHideMenuBar -bool true

# 取消 Dock 默认显示的“最近打开应用”
defaults write com.apple.dock show-recents -bool false

# 调整 Dock 图标大小
defaults write com.apple.dock tilesize -int 58

# 禁止 App 开启时在 Dock 弹跳
defaults write com.apple.dock no-bouncing -bool true

# 隐藏 Dock 图标 indicator lights
defaults write com.apple.dock show-process-indicators -bool false

# 隐藏 App 的图标半透明
defaults write com.apple.dock showhidden -bool true

# Unpin 全部 App
defaults write com.apple.dock persistent-apps -array ""

########### Finder

# 显示所有扩展名,显示隐藏文件
defaults write -g AppleShowAllExtensions -bool true
defaults write com.apple.finder AppleShowAllFiles -bool true

# 禁用修改扩展名时的警告
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false

# 显示底部地址栏
defaults write com.apple.finder ShowPathbar -bool true

# 所有目录默认多列布局
defaults write com.apple.finder FXPreferredViewStyle -string clmv

# 按文件名排序时,优先显示目录
defaults write com.apple.finder _FXSortFoldersFirst -bool true

# 默认搜索范围,改为当前目录
defaults write com.apple.finder FXDefaultSearchScope -string SCcf

# 设置 Finder 打开时默认位置为 $HOME
defaults write com.apple.finder NewWindowTarget -string PfHm
defaults write com.apple.finder NewWindowTargetPath -string "file://$HOME/"

# 允许选择 Quick Look 中的文本
defaults write com.apple.finder QLEnableTextSelection -bool true

# 保存窗口默认展开,不再多点一次小三角
defaults write -g NSNavPanelExpandedStateForSaveMode -bool true
defaults write -g NSNavPanelExpandedStateForSaveMode2 -bool true

# network volumes 禁止创建 .DS_Store
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true

# 文件详情中,展示 MetaData,不展示 Preview
defaults write com.apple.finder FXInfoPanesExpanded -dict MetaData -bool true Preview -bool false

########### Apps

# 修改 screenshot 默认文件名
defaults write com.apple.screencapture name -string screenshot
defaults write com.apple.screencapture include-date -bool false

# 开启 Safari debug 相关功能
defaults write com.apple.Safari IncludeInternalDebugMenu -bool true
defaults write com.apple.Safari IncludeDevelopMenu -bool true
defaults write com.apple.Safari WebKitDeveloperExtrasEnabledPreferenceKey -bool true
defaults write com.apple.Safari com.apple.Safari.ContentPageGroupIdentifier.WebKit2DeveloperExtrasEnabled -bool true

# Activity Monitor 显示所有进程,并按 CPU usage 排序
defaults write com.apple.ActivityMonitor ShowCategory -bool false
defaults write com.apple.ActivityMonitor SortColumn -string CPUUsage
defaults write com.apple.ActivityMonitor SortDirection -bool false

上面执行完,注销重新登入即可生效,或 killall Dockkillall Finder。最后允许打开任何来源的应用:

sudo spctl --master-disable

电源管理

# 20 分钟进入睡眠
sudo pmset -a sleep 20
# 30 分钟硬盘睡眠
sudo pmset -a disksleep 30
# 10 分钟屏幕睡眠
sudo pmset -a displaysleep 10

# 禁止网络唤醒
sudo pmset -a womp 0
# 禁止接通 AC 时唤醒
sudo pmset -a acwake 0
# 禁止被同网络、同 iCloud 其它设备唤醒
sudo pmset -a proximitywake 0

# 睡眠时不保持网络连接,Find My Mac 等服务会因此失效
sudo pmset -a tcpkeepalive 0
# 禁止 asleep 时执行更新、备份等任务
sudo pmset -a powernap 0

# 进入睡眠时,有“全亮-半亮-关闭”的屏幕过渡
sudo pmset -a halfdim 1
# 将内存数据同步到硬盘,一段时间后停止内存供电
sudo pmset -a hibernatemode 3
# battery 时自动切换 GPU,charger 时使用独立 GPU
sudo pmset -b gpuswitch 2
sudo pmset -c gpuswitch 1

# sleep 一段时间后自动 hibernate
sudo pmset -a standby 1
# 高电量(> highstandbythreshold),2 小时 hibernate
sudo pmset -a standbydelayhigh 7200
# 低电量(< highstandbythreshold),1 小时 hibernate
sudo pmset -a standbydelaylow 3600
# highstandbythreshold 默认为 50%

Xcode Command Line Tools

设置完后,第一件事是安装 Xcode Command Line Tools:

xcode-select --install

安装后,git 就可以使用了。以防万一,这里再记一个 reinstall 方法:

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

安装 Homebrew

系统所有 App 都使用 Homebrew 管理,安装它:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

对于上架 Mac App Store 的 App,通过 mas 优先从 Store 下载。用 brew 安装它:

brew install mas

这样所有 App 的自动检查更新都可以关掉,然后将下面 script 加到 crontab 里即可实现定时自动更新:

brew update
brew upgrade --greedy-auto-updates
brew cleanup --prune=all

mas upgrade

安装常用软件

浏览器

brew install --cask google-chrome
brew install --cask firefox
brew install --cask microsoft-edge

brew tap homebrew/cask-versions
brew install --cask google-chrome-canary

安心冲浪

brew install --cask adguard

密码管理

brew install --cask 1password

编程语言

brew install go
brew install python
brew install node pnpm
brew install php composer
brew install --cask temurin

# Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

终端工具

brew install --cask iterm2
brew install coreutils
brew install tmux neovim ranger
brew install exa zoxide fzf bat tldr tig dust btop fd tree unar
brew install jless jq jc duti
brew install starship
brew install ffmpeg
brew install nali dog httpie nmap telnet
#brew install subversion
brew install upx brotli
composer g require psy/psysh  # interactive shell for php

UI 工具

brew install --cask alfred
brew install --cask spotify
brew install --cask mpv
brew install --cask iina
brew install --cask maczip
brew install --cask parallels
brew install --cask aerial  # screen saver
brew install --cask bartender
mas install 441258766  # Magnet
mas install 1452523807  # Text Scanner
#mas install 1453504509  # Dynamic Wallpaper Engine

开发工具

brew install --cask visual-studio-code
brew install --cask intellij-idea goland webstorm
brew install --cask fork
brew install --cask sequel-ace
brew install --cask navicat-premium
brew install --cask docker
brew install --cask transmit
brew install --cask postman
brew install --cask coderunner
brew install --cask dash
brew install --cask wireshark
#brew install --cask charles
brew install --cask proxyman
brew install --cask beyond-compare

开发工具(偶尔用一次)

brew install --cask paw
brew install --cask altair-graphql-client
brew install --cask another-redis-desktop-manager
#brew install git-ftp
mas install 1296084683  # Cleaner for Xcode

即时通讯

brew install --cask telegram
brew install --cask discord
brew install --cask element
#brew install --cask lark
mas install 451108668   # QQ
mas install 836500024   # WeChat
#mas install 1435447041  # DingTalk

下载工具

brew install aria2
brew install --cask downie

阅读写作

brew install hugo
brew install --cask notion
brew install --cask raindropio
brew install --cask koodo-reader
mas install 1529448980  # Reeder 5

图片视频

brew install graphviz
brew install --cask snipaste
brew install --cask kap
brew install --cask omnigraffle
brew install --cask screenflow
brew install --cask obs
brew install --cask sip
brew install --cask sf-symbols

文件同步

mas install 823766827  # OneDrive

常用字体

brew tap homebrew/cask-fonts  # You only need to do this once!
brew install --cask font-sf-mono
brew install --cask font-fira-code-nerd-font
brew install --cask font-roboto-mono-nerd-font font-roboto-mono-for-powerline
brew install --cask font-dejavu-sans-mono-nerd-font font-dejavu-sans-mono-for-powerline
brew install --cask font-source-code-pro font-source-code-pro-for-powerline
brew install --cask font-hack-nerd-font

不见得用

mas install 966085870   # TickTick
mas install 824183456   # Affinity Photo
mas install 1295203466  # Microsoft Remote Desktop
brew install --cask qt-creator
brew install --cask wechatwebdevtools
brew install --cask baidunetdisk
brew install --cask hbuilderx

配置 SSH

SSH Key 全部由 1Password 管理。修改 ~/.ssh/config 文件:

Host *
	IdentityAgent "~/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock"

然后配置 Git 鉴权、签名,修改 ~/.gitconfig 文件:

[user]
	name = 33
	email = 33@gmail.com
	signingkey = ssh-ed25519 AAAAC3N...

[gpg]
	format = ssh

[gpg "ssh"]
	program = /Applications/1Password.app/Contents/MacOS/op-ssh-sign

[commit]
	gpgsign = true

[push]
	autoSetupRemote = true

设置默认应用

duti -s com.microsoft.VSCode .txt all
duti -s com.microsoft.VSCode .ass all
duti -s com.colliderli.iina .mp4 all

iTerm2

  • Profiles - General,Working Directory 选择 Reuse previous session's directory
  • Profiles - Text,Font 设置为 SF Mono, Regular, 16pt,勾选 Use a different font for non-ASCII text,并选择 Hack Nerd Font Mono, Regular, 16pt
  • Profiles - Window,调整 Transparency 为 10,调整 Blur 为 20
  • Profiles - Terminal,勾选 Unlimited scrollback
  • 下载 Dracula theme,在 Profiles - Colors,点 Color Presets…,然后点 Import… 导入并激活
# 使用 compact theme
defaults write com.googlecode.iterm2 TabStyleWithAutomaticOption -int 6

# 退出时不提示
defaults write com.googlecode.iterm2 PromptOnQuit -bool false

Keys

在 Profiles - Keys 中添加如下设置。

Option-Left:向前一个 word

  • Keyboard Shortcut: ⌥←
  • Action: Send Escape Sequence
  • Esc + b

Option-Right:向后一个 word

  • Keyboard Shortcut: ⌥→
  • Action: Send Escape Sequence
  • Esc + f

Option-Delete:删除一个 word

  • Keyboard Shortcut: ⌥⌫
  • Action: Send Hex Code
  • 0x17

Fish Shell

使用以下命令安装,并设置为默认 Shell:

brew install fish
echo $(which fish) | sudo tee -a /etc/shells
chsh -s $(which fish)

之后重启终端,执行 fish_config,进入配置界面,在 colors tab 选择 Dracula 保存,然后切到 prompt tab 选择 Arrow 保存。

编辑 ~/.config/fish/config.fish 文件,加入:

# Alias
alias p="pwd"
alias o="open ."
alias l="exa --icons --group-directories-first"
alias ls="exa -al --icons --group-directories-first"
alias cat="bat"
alias du="dust -r -n 999999999"
alias icpng="mkdir converted-images; sips -s format png * --out converted-images"
alias icjpg="mkdir converted-images; sips -s format jpeg * --out converted-images"

# Paths
set -g fish_user_paths ~/.cargo/bin $fish_user_paths
#set -g fish_user_paths /opt/homebrew/bin $fish_user_paths

# Functions
function pb
	git add -A
	git commit -m "chore: publish"
	git push
end
function tc
	set TEMP_DIR $(mktemp -d)
	cd $TEMP_DIR
	code $TEMP_DIR
end

# Initialize for tools
starship init fish | source
zoxide init fish | source

安装某些工具的自动完成:

pnpm install-completion fish

最后安装 Fisher,和几个常用插件:

curl -sL https://git.io/fisher | source && fisher install jorgebucaran/fisher

fisher install jhillyerd/plugin-git
fisher install jethrokuan/z
fisher install PatrickF1/fzf.fish

Squirrel

输入法使用开源的 Squirrel,安装它:

brew install --cask squirrel

安装后在 System Settings 删除所有 Input Sources,只保留 ABC 和 Squirrel 两个,前者系统限制删不掉。

然后克隆 rime-config

rm -rf ~/Library/Rime
git clone git@github.com:sxyazi/rime-config.git ~/Library/Rime

切换到 Squirrel,按 Ctrl-Option-` 重新部署。最后下载 sxyazi/a11y 按键辅助工具,该工具包含以下功能:

  • 自动切输入法:切换 App 时,自动将输入法切到 Squirrel。也就是对所有 App 都使用 Squirrel,至于不同 App 的默认中/英模式,在 Squirrel 配置中指定
  • 充分利用 R-Command:将其映射为 F19,目前将 Alfred 的呼出 hotkey 设置为 F19,Alfred Universal Actions 设置为 F19-L
  • 特定于应用的快捷键:如在 Finder 中按下 Option-T、Option-C,分别对应在当前目录打开 iTerm2、VSCode

Alfred

Universal Actions 是 Alfred 最强大的功能,也是主要卖点。将 sxyazi/alfred-workflows 导入,其中包括:

  • 实时汇率转换
  • UUID 生成
  • 时间日期转换
  • 常见编解码
  • MD5、SHA1、SHA256 计算
  • 图片压缩(TODO)

Logitech Unifying

鼠标 Bluetooth 会受 2.4Ghz Wi-Fi 影响,使用 Logitech 的 Unifying Receiver 会好很多。

它的配对软件没有 Homebrew formula,最后更新还在 2018,手动下载安装它:https://support.logi.com/hc/en-us/articles/360025297913

其它

其它 mpv tmux neovim ranger 这堆东西的配置,等有空再整理 :(