在你的Mac上安裝iTerm2 + oh my zsh
最近工作用的MBP越跑越慢,硬碟也快被一些專案塞爆了
拿了一台新電腦來用難免有一些工具要重新安裝,這時我習慣用terminal下指令操作
但Mac原生的terminal介面實在陽春到不行,也沒有autocomplete用起來實在是痛苦,所以首要任務就是安裝iTerm2 + oh my zsh不然很難再繼續下去😂
一、安裝iTerm2
- 下載iTerm2
前往iTerm2官方網站下載最新版本的iTerm2安裝包。 - 安裝iTerm2
下載完成後,打開安裝包,將iTerm2拖入「應用程式」文件夾中。 - 啟動iTerm2
打開應用程式中的iTerm2,確認其正常啟動。
二、安裝homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安裝完可以執行以下指令看看是否安裝完成
brew --version
有時使用brew install會出現以下錯誤
zsh: command not found: brew install xxx
那是因為安裝完成其實有一段是要設定brew的環境變數
- Run these three commands in your terminal to add Homebrew to your PATH:
echo '# Set PATH, MANPATH, etc., for Homebrew.' >> ~/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
他預設是加到 /.zprofile
~ 裡面,如果有 ~/.zshrc
加到這裡也可以
執行完重新開啟terminal才會生效
三、若沒有zsh就安裝一下
- 重啟終端機確保Zsh已成為預設的Shell。
- 設置Zsh為預設Shell
執行以下指令將Zsh設為預設Shell:
chsh -s /bin/zsh
- 檢查Zsh版本
macOS通常預裝Zsh,但可能不是最新版本。執行以下指令檢查版本:
zsh --version
- 如果版本過低,可以使用Homebrew更新:
brew install zsh
四、安裝Oh My Zsh
使用curl安裝,執行以下指令:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
或使用wget安裝:
sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
安裝完成後,終端機會自動切換到Oh My Zsh的預設主題。
五、安裝及配置主題與外掛
oh my zsh有許多的主題可以選擇,可以到 https://github.com/ohmyzsh/ohmyzsh/wiki/themes 選擇你喜歡的主題
假設選擇apple
這個主題,到.zshrc
中修改主題設定:
ZSH_THEME="apple"
儲存之後重新打開iTerm2劑會是新的主題了
然後要安裝 zsh-autosuggestions
和zsh-syntax-highlighting
# 安裝zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 安裝zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
然後編輯.zshrc
文件,啟用外掛:
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
儲存後執行以下指令使設定生效:
source ~/.zshrc
最後要來介紹oh my zsh裡面的git aliases
像我常常要建新分支就會用
git checkout -b brahcn_name
這個指令,但使用oh my zsh的git alias就只需要輸入
gcb branch_name
要推送到遠端分支原本是
git push upstream
這時只要輸入
gpu
即可
不過這個就很看開發者習慣
有的人很堅持要打完整的指令才覺得是做完一件事,但我覺得可以節省一點時間也是蠻不錯的,只是指令太精簡有時容易搞錯就是了😂
詳細的git aliases可以參考這裡 https://kapeli.com/cheat_sheets/Oh-My-Zsh_Git.docset/Contents/Resources/Documents/index