如果你不是通過搜索引擎搜到這篇文章的話,也許你還沒有聽說過powerline。而你又恰巧是個*UNIX黨,或者VIM黨的話,那麼強烈建議你了解並使用powerline。powerline是一個stateless status line,即一個全局狀態/提示欄。如果你成功為你的bash
,Terminal
,iTerm2
,VIM
配置上powerline的話,那麼效果將會是這樣的:
Bash的提示符將會是這樣的:
VIM的狀態欄將會是這樣的:
VIM的整體效果圖:
相信看了以上幾個截圖後,powerline的功能也就不言而喻了。他提供了各個app各個環境下的狀態提示,極大的提高了工作效率
首先我們需要下載安裝powerline。在正式安裝之前先啰嗦幾句powerline的代碼結構,github上的powerline項目下涵蓋 了用於適配各種APP(bash,vim等)的代碼。因此,你完全可以在Mac任何一個地方下載該代碼包,然後將不同的APP配置使用這個路徑,以 Plugin形式加載。為了方便讀者選擇性安裝,本文對於不同的程序將分開給出安裝路徑和配置。
先確定本機環境有一套版本大於等於2.7
的Python
的環境。如果沒有合適環境的話,可以通過homebrew安裝,這裡不多做贅述。
shell> python -v Python 2.7.9
然後通過pip
安裝powerline:
shell> pip install powerline-status
安裝完成後通過pip show powerline-status
查看powerline所處的具體路徑。注意:這個路徑很重要,會用在之後的配置環節
shell> pip show powerline-status Name: powerline-status Version: 2.0 Location: /Library/Python/2.7/site-packages Requires:
配置方法很簡單,只需要在Bash配置文件(例如:/etc/bashrc
,~/.bashrc
,~/.bash_profile
)中增加一行調用安裝路徑下的bindings/bash/powerline.sh
即可。這樣每次調用生成新的Bash窗口時,都會自動執行powerline.sh
文件中的內容。下面以~/.bash_profile
為例:
shell> echo << EOF >> ~/.bash_profile . /Library/Python/2.7/site-packages/powerline/bindings/bash/powerline.sh EOF shell> . /Library/Python/2.7/site-packages/powerline/bindings/bash/powerline.sh
注意:根據python安裝方式的不同,你的powerline所在路徑也可能不同。如果你是通過python官網或者apple store通過安裝工具安裝的python,那麼你的powerline安裝路徑就是/Library/Python/2.7/site-packages/powerline/
。如果你是通過brew install python
的話,那麼你的powerline路徑可能會有不同。請根據實際情況修改上面的命令。
執行完上面兩步後,不出意外powerline就已經開始工作了。但是你會發現Bash提示符會和下圖一樣是一些非常惡心的符號。
出現這樣情況的原因是powerline為了美觀自己造了一些符號,而這些符號不在Unicode字庫內(如果你不知道Unicode字庫是什麼的話可以看下博主以前的相關介紹)。所以想要powerline正常顯示的話,需要安裝特殊處理過的字體。好在有一位熱心人的幫助,他把大部分的程序猿常用的等寬字體都打上了powerline patch使得我們的這部配置將異常簡單。首先我們從github上下載並安裝字體:
shell> git clone https://github.com/powerline/fonts.git shell> cd fonts shell> ./install.sh
安裝完成後我們就可以在iTerm2
或者Terminal
的字體選項裡看到並選擇多個xxx for powerline
的字體了。*注意:對於ASCII fonts
和non-ASCII fonts
都需要選擇for powerline
的字體。如下圖:
這部分我們將介紹如何為VIM配置powerline。首先你需要確保你的vim編譯時開啟了python支持。如果通過python --version|grep +python
沒有結果的話,那麼你需要通過brew install vim --with-python --with-ruby --with-perl
重新編譯安裝vim,或者使用brew install macvim --env-std --override-system-vim
安裝macvim。
然後,你只需要在~/.vimrc
中加上以下部分,VIM就能夠正常加載powerline功能了:
注意:其中set rtp+=/Library/Python/2.7/site-packages/powerline/bindings/vim
和上文一樣需要按照自己的實際情況調整。
set rtp+=/Library/Python/2.7/site-packages/powerline/bindings/vim " These lines setup the environment to show graphics and colors correctly. set nocompatible set t_Co=256 let g:minBufExplForceSyntaxEnable = 1 python from powerline.vim import setup as powerline_setup python powerline_setup() python del powerline_setup if ! has('gui_running') set ttimeoutlen=10 augroup FastEscape autocmd! au InsertEnter * set timeoutlen=0 au InsertLeave * set timeoutlen=1000 augroup END endif set laststatus=2 " Always display the statusline in all windows set guifont=Inconsolata\ for\ Powerline:h14 set noshowmode " Hide the default mode text (e.g. -- INSERT -- below the statusline)原文:http://cenalulu.github.io/linux/mac-powerline/