Powerline:給 Vim 和 Bash 提供更棒的狀態行和提示信息
Powerline 是一個極棒的 Vim 編輯器的狀態行插件,這個插件是使用 Python 開發的,主要用於顯示狀態行和提示信息,適用於很多軟件,比如 bash、zsh、tmux 等等。
特色
- 使用 python 編寫,使其更具擴展性且功能豐富
- 穩定易測的代碼庫,兼容 python 2.6+ 和 python 3
- 支持多種 Linux 功能及工具的提示和狀態欄
- 通過 JSON 保存配置和顏色方案
- 快速、輕量級,具有後台守護進程支持,提供更佳的性能
Powerline 效果截圖

Vim 中 Powerline 狀態行效果
在本文中,我會介紹如何安裝 Powerline 及其字體,以及如何在 RedHat 和 Debian 類的係統中使 Bash 和 Vim 支持 Powerline。
第一步:準備好安裝 Powerline 所需的軟件
由於和其它無關項目之間存在命名衝突,因此 powerline 隻能放在 PyPI(Python Package Index)中的 powerline-status 包下.
為了從 PyPI 中安裝該包,需要先準備好 pip(該工具專門用於 Python 包的管理)工具。所以首先要在 Linux 係統下安裝好 pip 工具。
在 Debian、Ubuntu 和 Linux Mint 中安裝 pip
# apt-get install python-pip
示例輸出:
Reading package lists... DoneBuilding dependency treeReading state information... DoneRecommended packages:python-dev-all python-wheelThe following NEW packages will be installed:python-pip0 upgraded, 1 newly installed, 0 to remove and 533 not upgraded.Need to get 97.2 kB of archives.After this operation, 477 kB of additional disk space will be used.Get:1 http://archive.ubuntu.com/ubuntu/ trusty-updates/universe python-pip all 1.5.4-1ubuntu3 [97.2 kB]Fetched 97.2 kB in 1s (73.0 kB/s)Selecting previously unselected package python-pip.(Reading database ... 216258 files and directories currently installed.)Preparing to unpack .../python-pip_1.5.4-1ubuntu3_all.deb ...Unpacking python-pip (1.5.4-1ubuntu3) ...Processing triggers for man-db (2.6.7.1-1ubuntu1) ...Setting up python-pip (1.5.4-1ubuntu3) ...
在 CentOS、RHEL 和 Fedora 中安裝 pip
在 Fedora 類係統中,需要先打開 epel 倉庫,然後按照如下方法安裝 pip 包。
# yum install python-pip# dnf install python-pip [Fedora 22+ 以上]
示例輸出:
Installing:python-pip noarch 7.1.0-1.el7 epel 1.5 MTransaction Summary=================================================================================Install 1 PackageTotal download size: 1.5 MInstalled size: 6.6 MIs this ok [y/d/N]: yDownloading packages:python-pip-7.1.0-1.el7.noarch.rpm | 1.5 MB 00:00:01Running transaction checkRunning transaction testTransaction test succeededRunning transactionInstalling : python-pip-7.1.0-1.el7.noarch 1/1Verifying : python-pip-7.1.0-1.el7.noarch 1/1Installed:python-pip.noarch 0:7.1.0-1.el7Complete!
第二步:在 Linux 中安裝 Powerline
現在可以從 Git 倉庫中安裝 Powerline 的最新開發版。在此之前係統需要安裝好 Git 工具以便可以從倉庫拉下代碼。
# apt-get install git# yum install git# dnf install git
然後你可以通過 pip 命令安裝 Powerline。
# pip install git+git://github.com/powerline/powerline
示例輸出:
Cloning git://github.com/powerline/powerline to /tmp/pip-WAlznH-buildRunning setup.py (path:/tmp/pip-WAlznH-build/setup.py) egg_info for package from git+git://github.com/Lokaltog/powerlinewarning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'Installing collected packages: powerline-statusFound existing installation: powerline-status 2.2Uninstalling powerline-status:Successfully uninstalled powerline-statusRunning setup.py install for powerline-statuswarning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'changing mode of build/scripts-2.7/powerline-lint from 644 to 755changing mode of build/scripts-2.7/powerline-daemon from 644 to 755changing mode of build/scripts-2.7/powerline-render from 644 to 755changing mode of build/scripts-2.7/powerline-config from 644 to 755changing mode of /usr/local/bin/powerline-config to 755changing mode of /usr/local/bin/powerline-lint to 755changing mode of /usr/local/bin/powerline-render to 755changing mode of /usr/local/bin/powerline-daemon to 755Successfully installed powerline-statusCleaning up...
第三步:在 Linux 中安裝 Powerline 的字體
Powerline 使用特殊的符號來為開發者顯示特殊的箭頭效果和符號內容。因此你的係統中必須要有符號字體或者補丁過的字體。
通過下麵的 wget 命令下載最新的係統字體及字體配置文件。
# wget https://github.com/powerline/powerline/raw/develop/font/PowerlineSymbols.otf# wget https://github.com/powerline/powerline/raw/develop/font/10-powerline-symbols.conf
然後你將下載的字體放到字體目錄下 /usr/share/fonts 或者 /usr/local/share/fonts,或者你可以通過 xset q 命令找到一個有效的字體目錄。
# mv PowerlineSymbols.otf /usr/share/fonts/
接下來你需要通過如下命令更新你係統的字體緩存。
# fc-cache -vf /usr/share/fonts/
其次安裝字體配置文件。
# mv 10-powerline-symbols.conf /etc/fonts/conf.d/
注意:如果相應的符號沒有出現,可以嚐試關閉終端會話並重啟 X window,這樣就會生效了。
第四步:給 Bash Shell 和 Vim 狀態行設置 Powerline
在這一節將介紹 bash shell 和 vim 編輯器中關於 Powerline 的配置。首先通過在 ~/.bashrc 中添加如下內容以便設置終端為 256 色。
export TERM="screen-256color"
打開 Bash Shell 中的 Powerline
如果希望在 bash shell 中默認打開 Powerline,可以在 ~/.bashrc 中添加如下內容。
首先通過如下命令獲取 powerline 的安裝位置。
# pip show powerline-statusName: powerline-statusVersion: 2.2.dev9999-git.aa33599e3fb363ab7f2744ce95b7c6465eef7f08Location: /usr/local/lib/python2.7/dist-packagesRequires:
一旦找到 powerline 的具體位置後,根據你係統的情況替換到下列行中的/usr/local/lib/python2.7/dist-packages 對應的位置。
powerline-daemon -qPOWERLINE_BASH_CONTINUATION=1POWERLINE_BASH_SELECT=1. /usr/local/lib/python2.7/dist-packages/powerline/bindings/bash/powerline.sh
然後退出後重新登錄,現在 powerline 的狀態行應該如下顯示了。
Bash Powerline Statuslines
現在切換目錄並注意顯示你當前路徑的麵包屑導航提示的變化。
如果遠程 Linux 服務器上安裝了 powerline,你能看到後台掛起的任務,當你用 ssh 登錄上去時,會看到該提示增加了主機名。
在 Vim 中打開 Powerline
如果你喜歡使用 vim,正好有一個 vim 的強力插件。可以在 ~/.vimrc 中添加如下內容打開該插件(LCTT 譯注:注意同樣需要根據你的係統情況修改路徑)。
set rtp+=/usr/local/lib/python2.7/dist-packages/powerline/bindings/vim/set laststatus=2set t_Co=256
然後你打開 vim 後會看到一個新的狀態行:
原文發布時間為:2017-01-16
本文來自雲棲社區合作夥伴“Linux中國”
最後更新:2017-05-27 10:32:34

