效果圖
一、zsh特性
1、zsh是一款類似 bash的命令行解釋器shell,它可支持豐富的主題和插件,並幫助使用者更加便捷的使用linux系統,大部分兼容bash
2、特性
- 開箱即用、可編程的命令行補全功能可以幫助用戶輸入各種參數以及選項。
- 在用戶啟動的所有shell中共享命令歷史。
- 通過擴展的文件通配符,可以不利用外部命令達到find命令一般展開文件名。
- 改進的變量與數組處理。
- 在緩沖區中編輯多行命令。
- 多種兼容模式,例如使用/bin/sh運行時可以偽裝成Bourne shell。
- 可以定制呈現形式的提示符;包括在屏幕右端顯示信息,並在鍵入長命令時自動隱藏。
- 可加載的模塊,提供其他各種支持:完整的TCP與Unix域套接字控制,FTP客戶端與擴充過的數學函數。
- 完全可定制化。
- 其他還有很多功能通過使用插件實現
二、安裝
1、zsh支持任意的linux發型版本,詳細的不同操作系統安裝教程
Installing ZSH · ohmyzsh/ohmyzsh Wiki · GitHub
Plugins Overview · ohmyzsh/ohmyzsh Wiki (github.com) 插件總覽
apt-get install zsh
2、安裝 oh-my-zsh, 它是zsh的一個增強輔助,更好管理zsh,因為zsh包括了很多配置
sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
3、設置zsh為默認shell
chsh -s $(which zsh)
4、查看版本
zsh --version zsh 5.8 (x86_64-debian-linux-gnu)
5、查看安裝的主題和插件
ls ~/.oh-my-zsh/themes ls ~/.oh-my-zsh/plugins
6、配置
zsh的配置文件路徑在當前用戶下 如我的路徑是 ~/.zshrc,可以查看 cat ~/.zshrc
/ egrep -v "^[ \t]*#|^$" .zshrc export ZSH="$HOME/.oh-my-zsh" ZSH_THEME="random" plugins=(git wd web-search history history-substring-search zsh-autosuggestions z) source $ZSH/oh-my-zsh.sh export ZSH="$HOME/.oh-my-zsh" ZSH_THEME="agnoster" plugins=(git zsh-syntax-highlighting zsh-autosuggestions) source $ZSH/oh-my-zsh.sh source /root/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh source /root/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh alias k='kubectl' alias kgp='kubectl get pods' alias kgj='kubectl get jobs' alias kgs='kubectl get services' alias kgd='kubectl get deployment' alias kgds='kubectl get DaemonSet' alias kgss='kubectl get StatefulSets' alias klf='kubectl logs -f ' alias kgpv='kubectl get pv' alias kdp='kubectl delete pods' alias kdj='kubectl delete jobs' alias kcu='kubectl config use-context '
三、安裝命令補全插件
1、把插件倉庫克隆到$ZSH_CUSTOM/plugins
(默認位置是 ~/.oh-my-zsh/custom/plugins)
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
國內
git clone https://gitee.com/phpxxo/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
2、vim .zshrc
plugins=(git wd web-search history history-substring-search zsh-autosuggestions z)
source ~/.zshrc
3、autojump 插件也非常好使,提升效率杠杠滴
它的用法是輸入 j 目錄名
或 j 目錄名包含的字符
(這個目錄必須是之前 cd 訪問過的),就可直接切換到相應的目錄。不用再各種cd
啦~具體看下面截圖示例。
z 插件和 j插件的效果差不多
git clone git://github.com/wting/autojump.git cd autojump ./install.py
然后在~/.zshrc 里加上如下語句再source ~/.zshrc
即可生效
[[ -s /Users/xxxxxx/.autojump/etc/profile.d/autojump.sh ]] && source /Users/xxxxxx/.autojump/etc/profile.d/autojump.sh
注意:這個步驟在執行安裝后自動會提示,xxxxxx指代你的用戶名,到時候直接復制整句即可。
(若要卸載)
cd autojump ./uninstall.py
4、zsh-autosuggestions.zsh自動補全
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
5、再來一個高亮顯示常用命令的插件 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
,把zsh-syntax-highlighting
添加到 Oh My Zsh 的插件列表中
plugins=(git wd web-search history history-substring-search zsh-syntax-highlighting zsh-autosuggestions z)
6、extract 插件
功能強大的解壓插件,所有類型的文件解壓一個命令x
全搞定,再也不需要去記tar
后面到底是哪幾個參數了。
四、Powerlevel10k
簡單來說就是一個 ZSH
的主題,只不過它的功能很強大,以下簡稱 p10k
。
1、安裝
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/themes/powerlevel10k
2、然后需要打開 ~/.zshrc
設置 ZSH_THEME
:
ZSH_THEME="powerlevel10k/powerlevel10k
3、安裝字體 Nerd Fonts
https://www.nerdfonts.com/font-downloads 官網下載
你可以如官網所說,通過 brew
來安裝:
brew tap homebrew/cask-fonts brew cask install font-hack-nerd-font
但是我不建議這樣,包括不建議你下載 zip
包,因為這個文件太大了,太大了,太大了。。。
我們可以這樣:
打開 Release v3.0.1 · ryanoasis/nerd-fonts · GitHub,滑動頁面找到 Assets
區域,如圖:
我們只要下載箭頭所指的 Hack.zip
這個字體包,解壓縮之后就會獲得一些 ttf
字體文件,雙擊安裝即可。
zshrc 設置字體
POWERLEVEL9K_MODE="nerdfont-complete" ZSH_THEME="powerlevel10k/powerlevel10k"
注意,需要設置在 ZSH_THEME
之前。
iTerm2 設置字體
操作路徑:菜單欄 -> Profiles -> Open Profiles -> Edit Profiles -> 選擇 Text
自動配置
如果你指定了 ZSH_THEME="powerlevel10k/powerlevel10k"
但是在 zshrc
里沒進行任何手動的配置,打開 iTerm2
的時候就會觸發自動配置的流程。
也可以通過以下命令再次進入自動配置的流程:
p10k configure
后面幾個選項隨意,執行完命令之后,就會初始化 p10k
:在根目錄下生成 ~/.p10k.zsh
,並且在 ~/.zshrc
底部寫入:
如果想廢除 p10k
的配置,只需要刪除 ~/.p10k.zsh
,並且刪除上面這條命令即可。
自定義配置
如果你想當高玩,也可以在 ~/.zshrc
里手動配置 p10k
,或者在 ~/.p10k.zsh
基礎上進行修改。
這個得要自己看文檔摸索啦,這里我簡單說幾個配置:
- POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
- POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS
- POWERLEVEL9K_VCS_GIT_GITHUB_ICON
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
顯示在命令行左邊區域的元素:
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir dir_writable vcs virtualenv os_icon)
顯示在命令行右邊區域的元素:
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status background_jobs command_execution_time time)
可以在 POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
和 POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS
里用的字段有:
字段 |
含義 |
---|---|
user |
用戶名 |
dir |
當前目錄名 |
vcs |
遠程倉庫信息 |
os_icon |
系統圖標 |
date |
日期 |
host |
主機名 |
status |
上一條命令的執行狀態 |
time |
當前時間 |
... |
... |
如果還想了解更多,如下
GitHub - romkatv/powerlevel10k: A Zsh theme
Segment | Meaning |
---|---|
anaconda |
virtual environment from conda |
asdf |
tool versions from asdf |
aws |
aws profile |
aws_eb_env |
aws elastic beanstalk environment |
azure |
azure account name |
background_jobs |
presence of background jobs |
battery |
internal battery state and charge level (yep, batteries literally included) |
command_execution_time |
duration (wall time) of the last command |
context |
user@hostname |
cpu_arch |
CPU architecture |
dir |
current working directory |
direnv |
direnv status |
disk_usage |
disk usage |
dotnet_version |
dotnet version |
fvm |
flutter environment from fvm |
gcloud |
google cloud cli account and project |
goenv |
go environment from goenv |
google_app_cred |
google application credentials |
go_version |
go version |
haskell_stack |
haskell version from stack |
ip |
IP address and bandwidth usage for a specified network interface |
java_version |
java version |
jenv |
java environment from jenv |
kubecontext |
current kubernetes context |
laravel_version |
laravel php framework version |
load |
CPU load |
luaenv |
lua environment from luaenv |
midnight_commander |
midnight commander shell |
nix_shell |
nix shell indicator |
nnn |
nnn shell |
lf |
lf shell |
nodeenv |
node.js environment from nodeenv |
nodenv |
node.js environment from nodenv |
node_version |
node.js version |
nordvpn |
nordvpn connection status |
nvm |
node.js environment from nvm |
os_icon |
your OS logo (apple for macOS, swirl for debian, etc.) |
package |
name@version from package.json |
perlbrew |
perl version from perlbrew |
phpenv |
php environment from phpenv |
php_version |
php version |
plenv |
perl environment from plenv |
prompt_char |
multi-functional prompt symbol; changes depending on vi mode: ❯ , ❮ , V , ▶ for insert, command, visual and replace mode respectively; turns red on error |
proxy |
system-wide http/https/ftp proxy |
public_ip |
public IP address |
pyenv |
python environment from pyenv |
ram |
free RAM |
ranger |
ranger shell |
rbenv |
ruby environment from rbenv |
rust_version |
rustc version |
rvm |
ruby environment from rvm |
scalaenv |
scala version from scalaenv |
status |
exit code of the last command |
swap |
used swap |
taskwarrior |
taskwarrior task count |
terraform |
terraform workspace |
terraform_version |
terraform version |
time |
current time |
timewarrior |
timewarrior tracking status |
todo |
todo items |
toolbox |
toolbox name |
vcs |
Git repository status |
vim_shell |
vim shell (:sh ) |
virtualenv |
python environment from venv |
vi_mode |
vi mode (you don't need this if you've enabled prompt_char) |
vpn_ip |
virtual private network indicator |
wifi |
WiFi speed |
xplr |
xplr shell |
POWERLEVEL9K_VCS_GIT_GITHUB_ICON
如果它是一個 Github
目錄,就會顯示這個圖標:
所以出現在窗口里的圖標都可以自定義,可以通過命令查看目前正在使用的圖標
get_icon_names
找到想要修改的 KEY
就可以修改圖標了。
注意:需要使用
Nerd Fonts
才能收獲這滿滿的快樂。
有人問,這個圖標的代碼該去哪找呢?
在這里:?https://www.nerdfonts.com/cheat-sheet
這是 Nerd Fonts
能夠支持的所有圖標,可以直接使用關鍵字進行搜索。
比如,我想修改 Git
的圖標:
找到喜歡的圖標之后,右下角的 f113
就是這個圖標的值,只需要這樣就了:
POWERLEVEL9K_VCS_GIT_GITHUB_ICON=$'\uf113'
插件
到了這一步,你的 iTerm2
應該已經顏值爆表,足夠好看了。
畢竟這是我們的飯碗,光好看不行,得好用,來了解一下強大的插件體系。
首先,我們先了解一下插件在 ~/.zshrc
的哪個位置,找到下面這個字段就不會錯了
plugins=(git)
是不是簡單多了。可以通過命令查看所有配置:
vim ~/.oh-my-zsh/plugins/git/git.plugin.zsh
VS Code 配置
如果你用的是 VS Code
,需要再配置一下字體:
{ "terminal.integrated.fontFamily": "Hack Nerd Font" }
macOS安裝字體nerd-font - 掘金 (juejin.cn)
這篇 iTerm2 + Oh My Zsh 教程手把手讓你成為這條街最靚的仔-騰訊雲開發者社區-騰訊雲 (tencent.com)