[實作筆記] MacBook Terminal 美化與設計

前情提要

3 年前寫過如何讓 windows 也有美美命令提示視窗,
不過在去年我開始使用 MacBook Pro 了, 當然也想要美美的 Terminal 啦.
但是這個想法一直在放心裡面沒有實踐, 畢竟只是一個浮誇的東西.
不過在防疫期間重新看了一遍 高見龍的即將失傳的古老技藝 影片,
除了把 Vim 再熟悉一遍外, 同時也觸動了心中浮誇的那塊.
實作上比 Windows 簡單很多, 在這裡稍作記錄.

Overview

  • 下載 iTerm2
  • 設定 iTerm2
  • 安裝 oh-my-zsh
  • 安裝 powerlevel10k

第一步 下載並安裝 iTerm2

設定 iTerm2 的外觀

  1. 從網站 iTerm2-Color-Schemes (https://github.com/mbadolato/iTerm2-Color-Schemes) 下載你喜歡的配色方案,例如 “DimmedMonokai.itermcolors”。
  2. 開啟 iTerm2,點擊菜單欄的 “iTerm2”,選擇 “Preferences”。
  3. 在偏好設定視窗中,選擇 “Profiles” 選項卡,然後選擇你要更改配色方案的會話配置文件。
  4. 在 “Colors” 選項卡下,點擊 “Color Presets” 按鈕,選擇 “Import…”。
  5. 找到剛才下載的配色方案檔案,例如 “DimmedMonokai.itermcolors”,點擊 “Open”。
  6. 選擇 “DimmedMonokai” 作為你的 iTerm2 配色方案。

import .itemcolors

Profiles 裡有更多的設定, 字型、顏色
比如說, 調整啟始視窗大小與背景透明度, 可以前往 Windows 進行設定.
更多的細部設定可以自行摸索. 記得有些效果需要手動重啟 iTerm

第二步, 安裝 oh-my-zsh

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

安裝相當簡單, 更多資訊可以參考 Github
這裡要了解如何對 ~/.zshrc 進行編輯, 在後面安裝我們會需要編輯這個檔案,
可以確認一下檔案內容

1
vim ~/.zshrc

安裝 Powerlevel10k

前往 Powerlevel10k
我們使用 oh-my-zsh 的方式安裝

1
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

安裝後編輯 ~/.zshrc 設定參如下:

ZSH_THEME="powerlevel10k/powerlevel10k"

之後重啟 iTerm2 將會有一連串設定問題, 依照喜歡的設定即可,
可以參考這個影片,
如果設定完後不喜歡, 可以執行 p10k configure 重新設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    This is Powerlevel10k configuration wizard. It will ask you a few questions and
configure your prompt.

Does this look like a diamond (rotated square)?
reference: https://graphemica.com/%E2%97%86

--->  <---

(y) Yes.

(n) No.

(q) Quit and do nothing.

Choice [ynq]:

其它

這裡記錄一下我會使用的設定

~/.zshrc

1
ZSH_THEME="powerlevel10k/powerlevel10k" # 設定使用 powerlevel10k 主題

安裝完 oh-my-zsh  後如果有資料夾問題, 會出現以下訊息

1
2
3
4
5
6
7
8
9
10
11
12
13
[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.

[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh] compaudit | xargs chmod g-w,o-w

[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.

一般來說可以執行 compaudit | xargs chmod g-w,o-w 指令再重啟 iTerm2 即可以排除此問題.
如果無法排除, 又覺得每次出現這個訊息很煩人的話, 請至 ~/.zshrc 設定以下資訊

1
ZSH_DISABLE_COMPFIX="true"

活用 Terminal 的左右空間

在一般的終端機上, 隨著深入資料夾結構之中,
左側顯示路徑是會越來越長, 可以透過設定讓其顯示縮短, 更加的簡潔
編輯 ~/.p10k.zsh 以下參數

  • POWERLEVEL9K_SHORTEN_DIR_LENGTH = 1
  • POWERLEVEL9K_DIR_MAX_LENGTH = 0

而右側的留白空間, 反而很適合放入一些有用的參數
編輯 ~/.p10k.zsh 以下參數

  • POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS (右側的顯示內容)

未來如果有要調整相關的設定, 找.p10k.zsh.zshrc的相關設定就對啦

附上成果圖
my iTerm2

20211114 補充

但是在一些工具預設會用原本的 Terminal (終端機)開啟,
比如說 git-fork, 就會開啟原始的終端機,
如果沒有更換字型, 就會看到一些不正常顯示的?符號, 可以在
終端機 > 偏好設定 > 描述檔 > 文字 > 字體 作更改
我使用的字體是: MesloLGS NF
另外,git-fork 可以在

  • Preference > Integration > Terminal Client 調整開啟的終端機為 iTerm2

20211114 補充 2

我已經將 .vim 資料夾入版控,
未來只要在

  1. 在 ~ 目錄下 Clone .vim Repo 即可

  2. 在 ~ 目錄下設定連結

    ln ./.vim/.vimrc .

20211114 補充 3

讓命令呈現 highlight 語法,
使用 zsh-syntax-highlighting
這個設定可以帶來的好處是, 視覺上可以第一時間知道你有沒有打錯命令

Oh-my-zsh 的安裝方法

Clone this repository in oh-my-zsh’s plugins directory:

1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

編輯 ~/.zshrc 開啟外掛

plugins=( [plugins…] zsh-syntax-highlighting)

example:

plugins=( git zsh-syntax-highlighting)

最後重啟終端機即可

20211116 補充

如何讓錯誤訊息 Highlight,

iTerm > Help > Trigger 或是
iTerm > Preferences > Advanced > Trigger > Edit

以下是我的設定:

(?i:.error.) // Yellow on Red
(?i:.(warning|warn).) // Yellow
(?i:.FATAL.) // Red

20230419 補充

無限歷史行數,一般來說預設為 1000 行,
這個數量隨便執行一些語法就到頂了。
iTerm2 > Settings > Profiles > Terminal,打勾 Unlimited scrollback

參考

(fin)

Please enable JavaScript to view the Gitalk. :D
Please enable JavaScript to view the LikeCoin. :P
Please enable JavaScript to view the LikeCoin. :P