創作內容

2 GP

【macOS】現代終端機都這麼潮嗎?Iterm2、zshell、oh-my-zsh、powerlevel9k

作者:GJLMoTea│2020-07-07 23:52:49│巴幣:4│人氣:6203



完成品↓↓↓↓↓↓↓↓



你還是我所認識的終端機嗎...


這可不是只有畫面變 Colorful 了,
git 分支都能幫你顯示、甚至貓咪都出動了,
tab 鍵的效果也有進一步的功能,擁有更棒的寫程式體驗。



要變得這麼潮之前,要先安裝幾個東西,
雖然有點多、但別急,一步一步來。




用Homebrew 安裝 Iterm2

Iterm2 是具有像tmux指令那樣視窗分割、功能強大的 仿真Terminal,
當然執行速度上會比mac內建的 Terminal 稍慢一點,但在使用上便利性十足。
iTerm2 is an open source replacement for Apple's Terminal.
It's highly customizable and comes with a lot of useful features.
$ brew tap homebrew/cask

$ brew cask install iterm2



從此 Iterm 就能取代 mac 終端機了。









安裝 Powerline font字型


Powerline是用python開發出的 vim命令列與shell的外掛

Powerline is a statusline plugin for vim, and provides statuslines and prompts for several other applications, including zsh, bash, fish, tmux, IPython, Awesome, i3 and Qtile.

用Homebrew追蹤遠端cask-fonts
$ brew tap homebrew/cask-fonts
要先安裝svn才能安裝字體
$ brew install svn
搜尋有哪些powerline、nerd字體。
因為 nerd 收錄(patched)的特殊字元比powerline還要多,
所以若之後遇到Powerline無法顯示的亂碼時,可以轉成nerd試試

$ brew search powerline
$ brew search nerd

在這裡我們挑一個powerline的字體安裝
$ brew cask install font-source-code-pro-for-powerline

在這裡我們挑一個nerd的字體安裝
$ brew cask install font-sauce-code-pro-nerd-font

字體安裝完後先按command+Q退出 Iterm,再重新打開 Iterm


點選 Preference -> Profiles -> Text -> 選擇Font,就能更改字型啦~














安裝zshell

因為 macOS 2018 Mojave內建有zsh,可以不用做以下數行指令

$ brew install zsh
$ which zsh
/usr/local/bin/zsh
$ sudo sh -c "echo $(which zsh) >> /etc/shells"
$ chsh -s $(which zsh)

只要確認zsh存在於/etc/shells下,change shell就行了

$ cat /etc/shells
$ chsh -s /bin/zsh

退出shell重開,原本的$字號變成%符號,印出來看看
% echo $0
% echo $SHELL


此時在 Terminal中,按tab按鍵會有"遍歷檔案名稱"的效果








安裝 oh-my-zsh

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

原本%符號變成 → ~
→ ~ 表示路徑在HOME目錄下
→ / 表示在根目錄下
在其他目錄
此時可以看到 家目錄底下多了一個 ~/.oh-my-zsh 的資料夾


歐歐
安裝完後遇到問題,跳出一大串提示(沒問題的跳過這一段)
[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] 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.

因為安全的問題,
要禁用上述所列出來的檔案及資料夾的 group、others 的文件修改權限 (以確保只有root跟文件擁有者可以修改文件),或者直接禁用安全檢查。
換言之就是把提示列出的所有文件權限改成755 ($ chmod 755 ...),但因為比較麻煩,所以在這裡直接禁用檢查功能。


在.zshrc檔的第一行添加 ZSH_DISABLE_COMPFIX=true
→ ~ vim ~/.zshrc


再重新載入
→ ~ source ~/.zshrc


此時按tab按鍵會有"遍歷檔案名稱+選取顏色及反白"的效果

到這個步驟,若沒有安裝powerline外掛 都還不會有文字亂碼,
因為還沒有要顯示特殊字元








安裝 powerlevel9k 主題

powerlevel9k 是ZSH上很酷炫的主題,
可以在命令列顯示時間、電力、CPU memory使用率、其他電腦的資訊等等。
Powerlevel9k is a theme for ZSH which uses Powerline Fonts. It can be used with vanilla ZSH or ZSH frameworks such as Oh-My-Zsh, Prezto, Antigen, and many others.
下載 powerlevel9k
→ ~ git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

載下來後,
若要切換主題顏色,則修改.zshrc 裡面的ZSH_THEME參數
→ ~ vim ~/.zshrc
預設為 ZSH_THEME="robbyrussell"
可改成 ZSH_THEME="agnoster"
或可改成 randomyssimplebira 來玩看看
所有的主題配色可以在這裡找到 https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

重新載入、立即套用
→ ~ exec $SHELL




還有一大堆參數設定可調整,例如左側、右側出現的提示等等



參考網路上其他人的配置
(~/.zshrc)
ZSH_THEME="powerlevel9k/powerlevel9k"
# 左側
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir dir_writable vcs vi_mode)
# 右側
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status background_jobs history ram load time)
# 若當前登入的帳號為你的帳號 xxx,就不用特別顯示出來
DEFAULT_USER="g2020070116"
# 使用 nerd font 時可以顯示更多 icon。詳情請參考 powerlevel9k wiki
POWERLEVEL9K_MODE='nerdfont-complete'
prompt_context() {
   if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
     prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
   fi
}

→ ~ exec $SHELL
→ ~ source ~/.zshrc





現在已有最新版本 Powerlevel10k,
速度及效能比 powerlevel9k快上許多,且配置設定兼容powerlevel9k
以下三行指令 for 安裝 powerlevel10k Github文檔

若你尚未安裝P9K
$ git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k

$ echo 'source ~/powerlevel10k/powerlevel10k.zsh-theme' >>! ~/.zshrc

退出 Iterm 重開


$ git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k

修改.zshrc中的 ZSH_THEME參數為 powerlevel10k/powerlevel10k

$ sed 's/powerlevel9k/powerlevel10k/g' -i ~/.zshrc$ exec zsh

重新啟動ZSH


P10K有非常人性化的介面可以調配設定
$ p10k configure





個人的.zshrc (從g9k升級到g10k)設定:










zsh-autosuggestions

這是ZSH上的一個套件,在你Terminal輸入指令時會依照 history(以前輸入過的同樣指令)幫你自動完成。
It suggests commands as you type based on history and completions.
Requirements: Zsh v4.3.11 or later

下載autosuggestions到目錄底下的plugins位置

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

$ vim ~/.zshrc

找到.zshrc檔案中 plugins參數這一行(如果沒有的話自己新增)
plugins=(zsh-autosuggestions)
如果有多個套件,則以空格隔開
plugins=(git zsh-autosuggestions)

之後重新載入
$ source ~/.zshrc
便有自動完成提示的功能,當灰字出現時直接 按 →(方向鍵右)就能自動完成。


引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4841894
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

2喜歡★gjlmotea 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:【Chrome套件】用u... 後一篇:【Windows】【Go...

追蹤私訊切換新版閱覽

作品資料夾

colanncolann
【繪圖創作】【科嵐工作室】11週年! 2024/4/1 https://home.gamer.com.tw/creationDetail.php?sn=5909405看更多我要大聲說昨天00:04


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】