發表於

美化 macOS 的 Terminal - 使用 iTerm2 + Oh My Zsh + Powerlevel10k + 各種好用的插件

作者
目錄

fig01

介紹

本篇文章主要紀錄如何美化 macOS 的 Terminal 環境 ,讓平常工作起來更高效。

這邊會安裝以下套件:

  • Homebrew
  • iTerm2
  • Zsh
  • Oh My Zsh
  • Powerlevel 10k 主題
  • zsh-autosuggestions
  • zsh-syntax-highlighting
  • Zsh-z

安裝環境

這邊使用的環境配置為 macOS , 並使用 iTerm2 為我們的終端機。

安裝 Homebrew

Homebrew 是一個在 macOS 上很夯很方便的套件管理工具:

打開 Terminal 輸入以下指令安裝:

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

常用 Homebrew 指令

更新Homebrew套件清單
brew update
更新Homebrew已安裝的套件
brew upgrade
更新用Cask安裝的套件
brew upgrade --cask
清除Homebrew舊版的套件
brew cleanup
Homebrew安裝套件
brew install
Homebrew解除安裝套件
brew uninstall

安裝 iTerm2

iTerm2 是一個比內建 Terminal 更好的終端機。

打開 Terminal 輸入以下指令,用 Homebrew 的 Cask 來安裝:

brew install --cask iterm2

變更 iTerm2 顏色主題

更改顏色主題的方式為 iTerm → Preferences → Profiles → Colors → Color Presets... 。
我自己是使用預設的 Dark Background。

fig02

如果內建的不夠使用的話,
這邊有很多顏色主題可供下載使用: https://github.com/mbadolato/iTerm2-Color-Schemes

查看系統已安裝的 Shell

cat /etc/shells

若沒有看到 zsh ,安裝 zsh

brew install zsh

修改預設 Shell 使用 Zsh

chsh -s /bin/zsh

安裝 Oh My Zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

安裝 PowerLevel10k 主題

git clone https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k

安裝 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 使用者設定檔

open ~/.zshrc
  1. 修改 主題

ZSH_THEME="powerlevel10k/powerlevel10k"

  1. 使用 Plugins

plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

儲存。

  1. 應用修改過的 zshrc
source ~/.zshrc

Powerlevel10k 設定精靈 與 字型安裝

應用修改 ~/.zshrc 之後,因為第一次使用 Powerlevel10k,會自動啟動設定。

若是之前沒有安裝過 Pathed 過的字型,設定精靈會詢問是否要幫你安裝 Meslo Nerd Font
按造設定精靈的提示,安裝完字體之後會要求你重新開啟 iTerm2 。
這邊需要使用 Command + Q 關閉 iTerm2 之後重新開啟。

之後就會進入設定的畫面,按造你的需求完成設定即可。

手動安裝 Patched 過的字型

若是沒有碰到設定精靈詢問你安裝字型的問題,然後顯示是有亂碼的話

請下載並安裝以下四個字型:

安裝 wget 套件

brew install wget

下載字型

wget https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Regular.ttf &&
wget https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Bold.ttf &&
wget https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Italic.ttf &&
wget https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Bold%20Italic.ttf

下載完成打開字型檔案,點擊 Install Font 。

修改 iTerm2 的字型

更改字型的方式為 iTerm → Preferences → Profiles → Text → Font 。 把 Font 改成 MesloLGS NF

fig03

重新設定 Powerlevel10k

若之前沒有啟動 Powerlevel10k 設定小幫手,或是還需要重新設定 Powerlevel10k,

打開 Shell 輸入,

p10k configure

[ 選用 ] 安裝 Zsh-z 插件

類似於 autojump 的插件,比 cd 更快速地直接跳到想去的資料夾,且效能更好沒有一堆依賴包。

安裝

git clone https://github.com/agkozak/zsh-z $ZSH_CUSTOM/plugins/zsh-z

修改 zshrc 使用者設定檔

open ~/.zshrc
  1. 在 Plugins 增加 zsh-z

例如: plugins=(git zsh-autosuggestions zsh-syntax-highlighting zsh-z)

  1. 儲存。

  2. 應用修改過的 zshrc

source ~/.zshrc

使用方法

查看已知的資料夾位置

z

直接進入包含此字串的資料夾

cd /
z et

查看其他用法

z -h