Mac OS User Guide

macOS 简明用户手册

常用配置

环境变量

通过 ~/Library/LaunchAgents/environment.plist 文件,可设置程序启动时的环境变量:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>moha.online</string>
        <key>ProgramArguments</key>
        <array>
            <string>sh</string>
            <string>-c</string>
            <string>launchctl setenv DOCKER_HOST tcp://njun-2004.local:2375;
;</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>
命令 作用
launchctl load ~/Library/LaunchAgents/environment.plist 10.13+ 无效
launchctl start ~/Library/LaunchAgents/environment.plist
launchctl stop ~/Library/LaunchAgents/environment.plist
launchctl unload ~/Library/LaunchAgents/environment.plist

映射 HomeEnd

修改键盘映射,可以让 HomeEnd 键在 macOS 上的行为和 Windows 平台一致:

mkdir -p ~/Library/KeyBindings/
vim ~/Library/KeyBindings/DefaultKeyBinding.dict
{
    "\UF729"  = moveToBeginningOfLine:; // home
    "\UF72B"  = moveToEndOfLine:;       // end
    "$\UF729" = moveToBeginningOfLineAndModifySelection:; // shift-home
    "$\UF72B" = moveToEndOfLineAndModifySelection:;       // shift-end
}

终端 Terminal 需要在属性对话框中设置,Home  键设为 \033OHEnd  键设为 \033OF

Eclipse 则在 Preferences 对话框的 General > Keys 页面,绑定 HomeEnd 键到 Line Start 和 Line End。

静态路由

netstat -nr # 当前路由表

route -n add -net <Target IP Range>/16 <Gateway IP>

禁用 Swap

重启系统时按 + R 键进入恢复模式,执行 csrutil disable 关闭保护模式,重启后使用以下命令关闭/开启 Swap。

# 确认当前内存管理模式
sysctl -a vm.compressor_mode
# macOS 10.11+
## 禁用 Swap
sudo launchctl unload -wF /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
sudo rm /var/vm/swapfile*
## 启用 Swap
sudo launchctl load   -wF /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist

禁用睡眠

Mac Mini/Pro 之类的常开系统可以通过以下命令禁用睡眠功能。

sudo pmset -a hibernatemode 0 
sudo rm /private/var/vm/sleepimage

常用命令

字体相关

brew tap caskroom/fonts && brew cask install font-source-code-pro 安装 Adobe 码农字体

SVG 转 PNG

# 根据 SVG 文件生成 1000 像素的 png 缩略图
qlmanage -t -s 1000 -o . <SVG Filename>

硬件巡查

# 检查 CPU 型号。
sysctl -n machdep.cpu.brand_string

# 检查磁盘 S.M.A.R.T 状态
brew install smartmontools
smartctl -a disk0

查询侦听端口

sudo lsof -PiTCP -sTCP:LISTEN

Oh My Zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Brew

# 预编译包上交大镜像
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.sjtug.sjtu.edu.cn/homebrew-bottles

# 预编译包清华镜像
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# 使用上交大镜像
cd "$(brew --repo)"
git remote set-url origin https://mirrors.sjtug.sjtu.edu.cn/git/brew.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.sjtug.sjtu.edu.cn/git/homebrew-core.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://mirrors.sjtug.sjtu.edu.cn/git/homebrew-cask.git
# 还原官方镜像
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin https://github.com/Homebrew/homebrew-cask.git

常见错误

Warning: Failed to set locale category LC_NUMERIC to en_CN.
Warning: Failed to set locale category LC_TIME to en_CN.
Warning: Failed to set locale category LC_COLLATE to en_CN.
Warning: Failed to set locale category LC_MONETARY to en_CN.
Warning: Failed to set locale category LC_MESSAGES to en_CN.

# 添加如下语句并在 .bashrc 或 .zshrc 中引用
export LC_ALL=en_US.UTF-8

安装盘制作

sudo /Applications/Install\ macOS\ Sierra.app/Contents/Resources/createinstallmedia --applicationpath /Applications/Install\ macOS\ Sierra.app --volume /Volumes/<U盘名称>

常见错误

执行 git 报错

macOS 自带 git 工具,但有时升级 Xcode 后执行 git 会出现:

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

之类的错误,尝试以下命令重装 Xcode 命令行可以解决。

# 重装 xcode 命令行
xcode-select --install

sudo xcode-select -switch /
Author: njun
njun's picture
Updated: 2020/05/01