如何從 0 開始配置 Mac

題圖:兔子工程師。

昨天新的 MBP 終於到了,要從 0 開始配置成趁手的還是需要一些時間的,我總共花了 4 小時左右。在此記錄下,希望對大家有所幫助,這可以算時「裝了啥」的詳細版。如果我基於這篇文章再來一遍,應該可以減少到 2 小時以下。但是,應該不會再來一次了,自己的電腦走 TimeMachine 就好了。

安裝 ClashX,你懂的

略。

安裝 App 和 Cli 工具

1、安裝 HomeBrew 並用他安裝 App 和 Cli 工具。App 可以在 homebrew-cask — Homebrew Formulae 裏找有沒有,Cli 工具可以在 homebrew-core — Homebrew Formulae 找有沒有。

# 先開代理,不然會很慢(依賴第一步)
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890

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

# 用 HomeBrew 安裝 App,以下是我可以用 HomeBrew 安裝的 App 列表(以字母排序,方便你查找)
brew install --cask \  
  1password \  
  alfred \  
  battery-buddy \  
  coteditor \  
  docker \  
  eagle \  
  espanso \  
  figma \  
  google-chrome  \  
  gas-mask \  
  handbrake \  
  iina \  
  iterm2 \  
  karabiner-elements \  
  keepingyouawake \  
  keycastr \  
  licecap \  
  microsoft-remote-desktop \  
  obs \  
  obsidian \  
  setapp \  
  shottr \  
  sogouinput \  
  sourcetree \  
  telegram \  
  thor \  
  usr-sse2-rdm \  
  videofusion \  
  visual-studio-code \  
  webstorm \  
  zerotier-one

# 安裝 Cli 工具,以下是我的(以字母排序,方便你查找)
brew install \  
  autojump \  
  bat \  
  cmatrix \  
  commitzen \  
  deno \  
  diff-so-fancy \  
  fd \  
  ffmpeg \  
  fzf \  
  gh \  
  git \  
  httpie \  
  hub \  
  hyperfine \  
  imagemagick \  
  jq \  
  lazygit \  
  mkcert \  
  nvm \  
  pnpm \  
  the_silver_searcher \  
  tig \  
  tldr \  
  tree \  
  ugit \  
  wget

2、用 SetApp 安裝額外 App。

3、用 Mac App Store 安裝額外 App。

4、通過其他渠道安裝額外 App。

準備本地目錄

我在 ~/Documents 下建了兩個目錄(新電腦還沒施工完成,這裏還會補充更多目錄),Code 和 SoftwareConfiguration,長這樣。Code 用於存代碼;SoftwareConfiguration 用於保存各種軟件配置,我把 Alfred、Eagle 和 Espanso 的配置放在這裏。

配置 App

按這個順序會比較好。

1、Karabiner-Elements

參考 Karabiner-Element 配置 F19 鍵 - HackMD 。在 Karabiner-Elements complex_modifications rules 搜「Change caps_lock key」,import 後只保留一條和 F19 相關的,然後在命令行裏編輯「~/.config/karabiner/karabiner.json」,把剛纔那條規則的「caps_lock」換成「right_command」(兩處)。這樣你就把基本不會用到的「右⌘」廢物利用變成了「F19」鍵,然後你的快捷鍵組合會多很多。

如果你仔細看配置,會發現「F19」是由四個鍵「⌘⇧⌃⌥」組成的,在一些 App 的快捷鍵配置裏你會看到四個鍵,不要奇怪,這也是他。

2、Alfred

做幾個配置。1)開啓 Powerpack,2)修改快捷鍵爲剛纔配的「F19」,3)把老電腦的 Alfred 配置複製到 ~/Documents/SoftwareConfiguration/Alfred 下,然後在「Advanced」裏修改配置目錄指向他,你的 Workflow 就全回來了,4)「Features > Web Bookmarks」裏記得把「Google Chrome Bookmarks」選上,這樣就可以用 Alfred 模糊搜 Chrome 書籤,用於快速打開網站。

3、iTerm2 和 zsh

先配置 iTerm2,這是效果圖。1)Appearance 裏,Pane 裏不要「Show per-pane title bar with split panes」,Dimming 裏選上第一和第三個,2)Profiles 裏,Working Directory 裏選「Reuse previous session's directory」。

安裝 zsh 和 starship,starship 是 rust 寫的 prompt 工具,極快。

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
omz update
source ~/.zshrc
# starship 是 rust 寫的 prompt 工具,極快
brew install starship
echo 'eval "$(starship init zsh)"' >> ~/.zshrc

安裝 zsh 的插件,我個人用到了 zsh-autosuggestions、zsh-completions 和 fast-syntax-highlighting。

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
git clone https://github.com/zdharma-continuum/fast-syntax-highlighting.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/fast-syntax-highlighting

配置 ~/.zshrc,我的配置如下(略做刪減)。這裏有些 alias 是以 , 開頭的,因爲這樣你敲 , 然後按「Tab」就可以看到所有自己定義的命令了。爲啥有些沒有加 , ?歷史原因… 因爲其他都用習慣了就不改了。

# Disable brew auto update
export HOMEBREW_NO_AUTO_UPDATE=1
export ZSH="$HOME/.oh-my-zsh"

plugins=(
  # 不會 git 插件,因爲和我的 alias 設置衝突
  # git
  zsh-completions
  zsh-autosuggestions
  fast-syntax-highlighting
)

# Alias
alias ,ms="%PATH/TO/MY/SCRIPT%"
alias ,ip="ipconfig getifaddr en0"
alias ,sshconfig="vim ~/.ssh/config"
alias ,gitconfig="vim ~/.gitconfig"
alias b=",ms branch"
alias umi="/Users/%MY_USERNAME%/Documents/Code/github.com/umijs/umi/packages/umi/bin/umi.js"
# chore
alias br="bun run"
alias c='code .'
alias i='webstorm .'
alias cdtmp='cd `mktemp -d /tmp/sorrycc-XXXXXX`'
alias pi="echo 'Pinging Baidu' && ping www.baidu.com"
alias ip="ipconfig getifaddr en0 && ipconfig getifaddr en1"
alias cip="curl cip.cc"
alias qr='qrcode-terminal'
alias ee="stree"
alias hosts="vi /etc/hosts"
## system
alias showFiles="defaults write com.apple.finder AppleShowAllFiles YES && killall Finder"
alias hideFiles="defaults write com.apple.finder AppleShowAllFiles NO && killall Finder"
# cd
alias ..='cd ../'
alias ...='cd ../../'
alias ..l.='cd ../../ && ll'
alias ....='cd ../../../'
alias ~="cd ~"
alias -- -="cd -"
alias ll='ls -alhG'
alias ls='ls -G'
# git
alias git=hub
alias gp="git push"
alias gt="git status -sb"
alias ga="git add ."
alias gc="git commit -av"
alias gcr="git checkout master && git fetch && git rebase"
alias gclean="git reset --hard && git clean -df"
alias grebase="git fetch && git rebase -i"

## timelapse
## ref: https://www.reddit.com/r/mac/comments/wshn4/another_way_to_timelapse_record_your_mac_screen/
function record() {
  cd ~/screencapture/jpg;
  RES_WIDTH=$(/usr/sbin/system_profiler SPDisplaysDataType | grep Resolution);
  RES_WIDTH=(${RES_WIDTH:22:4});
  RES_WIDTH=$((RES_WIDTH/2));
  while :
  NOW=$(date +"%y%m%d%H%M%S");
  do screencapture -C -t jpg -x ~/screencapture/jpg/$NOW.jpg;
    sleep 7 & pid=$!
    NOW=$(date +"%y%m%d%H%M%S");
    wait $pid
  done
}
function movie() {
  NOW=$(date +"%y%m%d%H%M%S");
  cd ~/screencapture/jpg;
  cnt=0
  rm -rf .DS_Store;
  for file in *
    do
      if [ -f "$file" ] ; then
      ext=${file##*.}
      printf -v pad "%05d" "$cnt"
      mv "$file" "${pad}.${ext}"
      cnt=$(( $cnt + 1 ))
    fi
  done;
  rm -rf 00000.jpg;
  for pic in *.jpg;
    do convert $pic -resize 50% $pic;
  done;
  ffmpeg -r 24 -i %05d.jpg -b 20000k ~/screencapture/mov/$USER-$NOW.mov;
  rm -rf ./*.jpg;
}

function pfd() {
  osascript 2> /dev/null <<EOF
  tell application "Finder"
    return POSIX path of (target of window 1 as alias)
  end tell
EOF
}
function mcd {
  mkdir $1 && cd $1;
}
function cdf() {
  cd "$(pfd)"
}
function ,touch {
  mkdir -p "$(dirname "$1")" && touch "$1"
}
function ,take() {
  mkdir -p "$(dirname "$1")" && touch "$1" && take "$(dirname "$1")"
}

# load zsh-completions
autoload -U compinit && compinit

# use nvm
source /opt/homebrew/opt/nvm/nvm.sh

# autojump
source /opt/homebrew/etc/profile.d/autojump.sh

# use starship theme (needs to be at the end)
eval "$(starship init zsh)"

# 必須在 plugins 之後
source $ZSH/oh-my-zsh.sh

# bun completions
[ -s "/Users/chencheng/.bun/_bun" ] && source "/Users/chencheng/.bun/_bun"

# bun
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"

# pnpm
export PNPM_HOME="/Users/chencheng/Library/pnpm"
export PATH="$PNPM_HOME:$PATH"

4、SSH

mkdir ~/.ssh
# file name 用 github,passphrase 隨意
ssh-keygen -t ed25519 -C "github"
# 編輯配置,內容如下
touch ~/.ssh/config
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/github
# 添加到系統 keychain
ssh-add --apple-use-keychain ~/.ssh/github
# 添加 public key 到 github
gh auth login
gh ssh-key add ~/.ssh/github.pub -t github

5、額外的命令行工具:Bun 和 Projj

安裝 Bun。我主要是用他的 run 命令,極快,上面也有別名 br,比如執行比如 br dev 即 npm run dev

curl -fsSL https://bun.sh/install | bash

安裝 PROJJ,我用他來管理 Code 下的倉庫,按「domain/group/repo」這樣組織,找起來會比較容易。

pnpm i projj projj-hooks -g
projj init

然後編輯 ~/.projj/config.json,我的配置如下(記得把 name 和郵箱改成自己的)。

{
  "base": [
    "/Users/%YOUR_USERNAME%/Documents/Code"
  ],
  "hooks": {
    "postadd": "projj_git_config_user"
  },
  "postadd": {
    "github.com": {
      "name": "sorrycc",
      "email": "sorrycc@gmail.com"
    }
  },
  "alias": {
    "github://": "https://github.com/"
  }
}

然後就可以愉快地用 PROJJ 添加項目了,比如。

projj add git@github.com:umijs/umi.git

6、Espanso

我在 ~/Documents/SoftwareConfiguration/Espanso 下建了個 base.yml,內容如下(已刪除個人敏感信息),並軟鏈到 Espanso 原來的配置文件夾裏。

matches:
  # misc
  - trigger: ";>>"
    replace: ➡
  - trigger: ";vv"
    replace: ⬇
  - trigger: ";^^"
    replace: ⬆
  - trigger: ";<<"
    replace: ⬅
  # life
  - trigger: ";mobi"
    replace: 我的手機號
  - trigger: ";mail"
    replace: 我的郵箱
  - trigger: ";addr"
    replace: 我的家庭住址
  - trigger: ";officeAddr"
    replace: 公司地址
  # faq
  - trigger: ";chongt"
    replace: 衝突了,merge 下 master。
  # code
  - trigger: ";log"
    replace: console.log($|$)
  - trigger: ";delay"
    replace: const delay = (ms) => new Promise((res) => setTimeout(res, ms));
  # mac symbols
  - trigger: ":cmd"
    replace: "⌘"
  - trigger: ":shift"
    replace: "⇧"
  - trigger: ":ctrl"
    replace: "⌃"
  - trigger: ":alt"
    replace: "⌥"
  - trigger: ":opt"
    replace: "⌥"
  - trigger: ":left"
    replace: "←"
  - trigger: ":right"
    replace: "→"
  - trigger: ":up"
    replace: "↑"
  - trigger: ":down"
    replace: "↓"
  - trigger: ":caps_lock"
    replace: "⇪"
  - trigger: ":esc"
    replace: "⎋"
  - trigger: ":eject"
    replace: "⏏"
  - trigger: ":return"
    replace: "↵"
  - trigger: ":enter"
    replace: "⌅"
  - trigger: ":tab"
    replace: "⇥"
  - trigger: ":backtab"
    replace: "⇤"
  - trigger: ":pgup"
    replace: "⇞"
  - trigger: ":pgdown"
    replace: "⇟"
  - trigger: ":home"
    replace: "↖"
  - trigger: ":end"
    replace: "↘"
  - trigger: ":space"
    replace: "␣"
  - trigger: ":del"
    replace: "⌫"
  - trigger: ":fdel"
    replace: "⌦"

7、Thor

讓你可以一鍵啓動、顯示或隱藏某個 App,對我來說是效率神器。但有時太快也不好,會讓你無意間地快速切過去,比如釘釘、Reeder 和 Telegram 我後來就把他們去掉了。

我的配置見圖,快捷鍵供參考。

8、安裝字體

編程字體我用 Monolisa,之前還用過 Source Code Pro、Dank Mono 和 Operator Mono。此外我還安裝了霞鶩文楷和阿里普惠體,霞鶩文楷我用在了語雀等文檔站和 Obsidian 裏。

9、WebStorm

簡單幾步配置即可。1)安裝 Github Copilot 和 Inspection Lens 插件,2)配置 Color Schema 爲「Intellij Light」,3)配置 Font 爲 MonoLisa,同時 Size 爲 20,大點對眼睛好,哈哈。

10、VSCode

略。輔助編輯器,還沒配。

主題是 Nako - Visual Studio Marketplace。

11、Git

先配 name 和 email。

git config --global user.name "Your Name"
git config --global user.email "you@your-domain.com"

再執行這兩條命令。

git config --global --add push.default current
git config --global --add push.autoSetupRemote true

你會收穫兩個好處。1)不需要「git push origin xxx」,只要「git push」,2)再也不會遇到「no upstream branch」的報錯,也不需要「git push --set-upstream origin test && git push」。因爲我們執行 git push 的大部分場景都是 push 到同名的 remote branch。來源是 Auto setup remote branch and never again see an error about the missing upstream | pawelgrzybek.com。

再修改 ~/.gitignore_global,加入和你 IDE 相關的 ignore 配置。我會把 .idea 加進去,這是和你相關的專有配置,如果給其他用 VSCode 的作者的項目提交時,都加上 .idea 的 .gitignore 配置,其實並不太禮貌。反之,VSCode 或其他編輯器工具的用戶也要加上自己的。

*~
.DS_Store
.idea

12、NVM 和 Node

nvm install 18
node -v

13、Eagle

Eagle 的庫我保存在 ~/Documents/SoftwareConfiguration/Eagle 下。

14、Focus

要稍微做下配置。1)添加 Block App,我加了釘釘、Reeder,2)Block Website 裏我把內置的全刪了,留了這些,3)Block App 的配置要選「強制退出」,「隱藏」的模式在 Mac 13.2 下有問題,不會生效或者表現奇怪。

15、Bob。

我的快捷鍵是「F19+A」劃詞翻譯,「F19+S」截圖翻譯。插件裝了 bob-plugin-deeplx 和 bobplugin-google-translate。文本翻譯我加了 Deepl X、有道、阿里、金山詞霸,文本識別我用騰訊 OCR。

16、Rectangle。

刪了所有快捷鍵,只保留兩個。「F19+C」居中,「F19+M」放最大。

17、uPic

用了自定義圖牀,略。

18、Paste

我的快捷鍵是「⌘⌥C」。配置裏選上「Always paste as Plain Text」,去掉「Enable sound effects」。

19、Reeder

兩個改動。在 Shortcuts 配置裏,把「Mark All as Read…」的快捷鍵改成「⇧A」,然後去掉「Ask before marking all as read」。

20、Google Chrome

登錄 Google 賬號後所有東西就都同步過來了,除了 Tampermonkey 的自定義腳本。但簡單 Google 後也找到了辦法,我參考 extract_tampermonkey_script.py · GitHub 把 /Users/%YOUR_USERNAME%/Library/Application\ Support/Google/Chrome/Default/Local\ Extension\ Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo 這個文件夾下的內容複製到新電腦後就能用了。

21、Telegram

登錄時死活登不上,開了代理纔行。

22、Obsidian

我先試了用 Obsidian Sync 直接同步出本地文檔庫,但發現只包含文檔,不包含插件。於是改成先用 git clone 完整倉庫,再關聯到 Obsidian Sync 的遠程文檔庫。用 Git 做同步時有個要注意的是,.obsidian 目錄下的 workspace 相關的 3 個文件不要提交,否則會很容易衝突。

23、SourceTree

配置中「Git」Tab 下選「Use System Git」,否則會報找不到 git 的錯誤。

24、iA Writer

把所有 Markdown 文件改成用 iA Writer 打開,因爲 iA Writer 又輕又好看。然後我在「系統設置 > Keyboard > Keyboard Shortcuts > App Shortcuts」中增加了一些針對 IA Writer 的快捷鍵配置。

25、Shottr

快捷鍵裏把所有都刪了,只保留兩個。Area screenshot 用「F19 + 7」,Scrolling screenshot 用「F19 + 8」。

26、Sip

Show Picker 的快捷鍵是「F19 + 2」。

27、CleanShotX

Capture Area 的快捷鍵是「F19 + 6」。

系統設置

1、General。1)Default Web Browser 用「Google Chrome」,2)Language & Region 裏,把 First day of week 改成「Monday」,這樣你的日曆就會從週一開始了。

2、Siri。直接禁掉。

3、Trackpad。Scroll direction:Natural 去掉。

4、Keyboard。1)Keyboard 裏把 Key Repeat 調到「Fast」,把 Delay Util Repeat 調到「Short」,需要一點時間適應,適應後會感受到光標快速移動帶來的效率提升,2)Text 裏 use " for double quotes,use ' for single quotes,然後把其他都禁掉,不需要系統幫忙改,基本都是幫倒忙的,3)Shortcuts 裏,Mission Control 用「⌥A」,Application windows 用「⌥S」,Show Desktop 用「⌥D」,Input Sources 的 Select Previous 用 「⌘Space」,Screenshots 裏 Save picture of selected area as a file 用「F19 + 3」,Copy picture of selected area to the clipboard 用「F19 + 4」,4)輸入法刪除默認的拼音改用搜狗拼音,登錄後可以在不同電腦之間同步詞庫,搜狗輸入法的皮膚我用的 Matrix 矩陣。

5、Spotlight。只開 Applications、Bookmarks & History、Documents、Folders、System Preferences。

6、Mission Control。把 Hot Corners 裏的全部關掉,不需要,因爲有 Thor 了,可以更快切除應用。

7、Sharing。只留「AirPlay Receiver」即可,同時可以改下 computer name。

8、Security & Privacy。把「Use Apple Watch to unlock」打開。

9、Notification。不必要的全關掉,我只開了 Calendar、Find By、Reminders 和 Wallet。

10、執行 defaults write -g NSWindowShouldDragOnGesture -bool true,然後就可以按住「⌘+⌃」然後鼠標點擊任意地方拖動窗口了。來源 Moving a macOS window by clicking anywhere on it (like on Linux) · mmazzarolo.com,但是在 MacOS 13 下似乎失效了。

參考

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/sdBZTSOzm94Zopgr_OijOg