1. 前言
對于管理人員和開發人員來說,一個好的終端可以給你帶來效率飛速的提升。
你還在用這樣的 bash 嗎?
或著用這樣的 終端?
是時候打造一款屬于你自己的終端配置啦!讓它兼具美觀好看和高效。
今天要介紹的終端美化方案是「zsh」+「oh my zsh」+「powerlevel10k」。你或許在百度上看到過 zsh+oh-my-zsh+powerlevel9k 的終端環境美化方案,它和我的美化方案沒什么大不同,只不過將 powerlevel9k 換成了powerlevel10k, 它是 9k 版本的強化版本,更快,性能更好。
接下去將一步步介紹安裝部署步驟。
2. 安裝 zsh
Linux 環境下的 shell 終端并不知有 bash,常見的還有 ksh,tcsh,zsh,fish等。其中大部分人用的是 bash,估計是因為大部分的 Linux 發行版默認的就是 bash 的緣故。我們來看一下各終端的使用占比
有些平臺默認就有 zsh 終端,只是默認不啟用,下面簡單介紹各環境下把 zsh 切換成默認的 shell 環境。
-
macos
macos下默認已經安裝了zsh,可以直接使用
# 終端輸入命令,然后輸入密碼即可將zsh切換成默認shell $ chsh -s /bin/zsh
-
redhat/centos
redhat家族默認好像都沒有安裝zsh,需要安裝
# 查看已有的shell終端 $ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /bin/tcsh /bin/csh /bin/ksh /bin/rksh # 安裝zsh $ yum install zsh # 終端輸入命令,然后輸入密碼即可將zsh切換成默認shell $ chsh -s /bin/zsh
-
ubuntu
# 安裝zsh $ sudo apt-get install zsh # 終端輸入命令,然后輸入密碼即可將zsh切換成默認shell $ chsh -s /bin/zsh
3. 安裝 oh my zsh
官網是這樣介紹 oh my zsh 的,然后詳細的文檔可以去 github 網址:https://github.com/robbyrussell/oh-my-zsh
Oh My Zsh is a delightful, open source, community-driven framework for managing your Zsh configuration. It comes bundled with thousands of helpful functions, helpers, plugins, themes, and a few things that make you shout...
Oh My Zsh可以做非常多的定制化功能,比如主題,字體,插件等。
簡單介紹如何安裝 oh my zsh
-
使用 curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
-
使用 wget
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
4. 安裝 powerlevel10k
4.1 為什么選擇powerlevel10k?
Oh My Zsh有上百個自帶主題,以及許許多多的外部主題。目前來看使用量教多的是 powerlevel9k,它的初始效果是這樣的。
這個主題我也用了挺長一段時間,用上它的感覺就是「媽的,雖然好用,但是響應速度讓我很不爽,明明沒有加載很多插件還是慢」。這個感覺直到我逛主題市場發現 powerlevel10k 后就再也不復存在了。
它的介紹是這樣的,注意關鍵字:兼容 Powerlevel9k,10-100倍的性能提升!
Powerlevel10k is a backward-compatible reimplementation of the popular Powerlevel9k theme (see above) with 10-100 times better performance. If you like the looks of Powerlevel9k but feeling frustrated by its slow prompt, simply replace your powerlevel9k theme with powerlevel10k and enjoy responsive shell like it's 80's again! Powerlevel10k uses the same configuration options as Powerlevel9k and produces the same results. It's simply faster.
簡單介紹如何安裝 powerlevel10k,詳細的教程自己去看:https://github.com/romkatv/powerlevel10k。
-
Oh My Zsh
由于我們使用的是Oh My Zsh,所以只需要將 github 上的 repo 下載到 Oh My Zsh 的目錄下然后配置即可。
git clone https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k # 然后設置 .zshrc 中的變量 ZSH_THEME Set ZSH_THEME=powerlevel10k/powerlevel10k in your ~/.zshrc.
5. 安裝字體
安裝完 powerlevel10k 后你可能會發現有亂碼,這是因為你終端字體不支持那么多的字符,這就需要使用擴展字體了。
字體的安裝,我們可以參考 powerlevel9k 的官方介紹:https://github.com/bhilburn/powerlevel9k/wiki/Install-Instructions#step-2-install-a-powerline-font
安裝 powerline 字體有四種方式:
Option 1: Install Powerline Fonts
不介紹,請看官方文檔
Option 2: Use a Programmer Font
不介紹,請看官方文檔
Option 3: Install Awesome-Powerline Fonts
不介紹,請看官方文檔
Option 4: Install Nerd-Fonts
Nerd 字體應該是支持字形最多的,所以不用多想,安裝它就好了!
github 上也有詳細的安裝介紹文檔:https://github.com/ryanoasis/nerd-fonts#font-installation
其中最簡單的應該是用 Homebrew,但是Homebrew在國內的訪問速度實在惡心,所以還是選擇手工安裝吧
直接上 nerdfonts 官網,找到Downloads,直接下載 Hack 字體,別問我為什么,如果是mac,直接雙擊安裝下載的字體即可,然后修改 oh my zsh 的變量文件 ~/.zshrc,修改變量 POWERLEVEL9K_MODE='nerdfont-complete'
, 如果不是MacOS,先自行查找如何配置 powerline 字體吧。
6. 常用插件
環境配置好了,我們還需要加上一些插件讓終端更好用,介紹我用的幾個插件,效率杠杠的
-
autojump
Github地址:https://github.com/wting/autojump
只要你在終端上使用 cd 訪問過的目錄,它都會記錄下來,然后下次可以在任何工作目錄下使用只需要用 j + 目錄名 就可以直接進入,比如
image-20190510162802485.png -
Zsh-autosuggestions
有了它,執行過的命令,就不需要重復的輸入啦,比如我輸入
source en
,它自動會提示曾今執行過的命令是source env/bin/activate
,這時候只需要點擊方向鍵的右健就可以直接填充命令,爽到爆有沒有。
May-10-2019 16-40-25.gif
7. 完整zshrc配置
實際效果
.zshrc文件配置
# Prompts
POWERLEVEL9K_LEFT_SEGMENT_SEPARATOR='\uE0C0'
#POWERLEVEL9K_LEFT_SUBSEGMENT_SEPARATOR='\uE0C0'
POWERLEVEL9K_RIGHT_SEGMENT_SEPARATOR='\uE0C2'
#POWERLEVEL9K_RIGHT_SUBSEGMENT_SEPARATOR='\uE0C2'
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir dir_writable vcs virtualenv)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status background_jobs command_execution_time ip)
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
POWERLEVEL9K_SHORTEN_DELIMITER=..
POWERLEVEL9K_PROMPT_ON_NEWLINE=true
POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX=$'\n'
POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX="\uF460%F{073}\uF460%F{109}\uF460%f "
export ZSH="/Users/john/.oh-my-zsh"
POWERLEVEL9K_MODE='nerdfont-complete'
ZSH_THEME="powerlevel10k/powerlevel10k"
export UPDATE_ZSH_DAYS=13
HIST_STAMPS="yyyy/mm/dd"
plugins=(
git
colored-man-pages
colorize
github
brew
osx
docker
docker-compose
autojump
zsh-autosuggestions
zsh-syntax-highlighting
autopep8
python
)
source $ZSH/oh-my-zsh.sh
alias zshconfig="vim ~/.zshrc"
alias vimconfig="vim ~/.vimrc"
alias ansibleconfig="vim ~/.ansible/ansible.cfg"
alias grep='grep --color=auto'