Powerline:給Vim和Bash提供更棒的狀態(tài)行和提示信息
Powerline 是一個極棒的 Vim 編輯器的狀態(tài)行插件,這個插件是使用 Python 開發(fā)的,主要用于顯示狀態(tài)行和提示信息,適用于很多軟件,比如 bash、zsh、tmux 等等。
特色
- 使用 python 編寫,使其更具擴展性且功能豐富
- 穩(wěn)定易測的代碼庫,兼容 python 2.6+ 和 python 3
- 支持多種 Linux 功能及工具的提示和狀態(tài)欄
- 通過 JSON 保存配置和顏色方案
- 快速、輕量級,具有后臺守護進程支持,提供更佳的性能
Powerline 效果截圖
Vim 中 Powerline 狀態(tài)行效果
在本文中,我會介紹如何安裝 Powerline 及其字體,以及如何在 RedHat 和 Debian 類的系統(tǒng)中使 Bash 和 Vim 支持 Powerline。
***步:準備好安裝 Powerline 所需的軟件
由于和其它無關(guān)項目之間存在命名沖突,因此 powerline 只能放在 PyPI(Python Package Index)中的 powerline-status 包下.
為了從 PyPI 中安裝該包,需要先準備好 pip(該工具專門用于 Python 包的管理)工具。所以首先要在 Linux 系統(tǒng)下安裝好 pip 工具。
在 Debian、Ubuntu 和 Linux Mint 中安裝 pip
- # apt-get install python-pip
示例輸出:
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- Recommended packages:
- python-dev-all python-wheel
- The following NEW packages will be installed:
- python-pip
- 0 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 類系統(tǒng)中,需要先打開 epel 倉庫,然后按照如下方法安裝 pip 包。
- # yum install python-pip
- # dnf install python-pip [Fedora 22+ 以上]
示例輸出:
- Installing:
- python-pip noarch 7.1.0-1.el7 epel 1.5 M
- Transaction Summary
- =================================================================================
- Install 1 Package
- Total download size: 1.5 M
- Installed size: 6.6 M
- Is this ok [y/d/N]: y
- Downloading packages:
- python-pip-7.1.0-1.el7.noarch.rpm | 1.5 MB 00:00:01
- Running transaction check
- Running transaction test
- Transaction test succeeded
- Running transaction
- Installing : python-pip-7.1.0-1.el7.noarch 1/1
- Verifying : python-pip-7.1.0-1.el7.noarch 1/1
- Installed:
- python-pip.noarch 0:7.1.0-1.el7
- Complete!
第二步:在 Linux 中安裝 Powerline
現(xiàn)在可以從 Git 倉庫中安裝 Powerline 的***開發(fā)版。在此之前系統(tǒng)需要安裝好 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-build
- Running setup.py (path:/tmp/pip-WAlznH-build/setup.py) egg_info for package from git+git://github.com/Lokaltog/powerline
- warning: 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-status
- Found existing installation: powerline-status 2.2
- Uninstalling powerline-status:
- Successfully uninstalled powerline-status
- Running setup.py install for powerline-status
- warning: 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 755
- changing mode of build/scripts-2.7/powerline-daemon from 644 to 755
- changing mode of build/scripts-2.7/powerline-render from 644 to 755
- changing mode of build/scripts-2.7/powerline-config from 644 to 755
- changing mode of /usr/local/bin/powerline-config to 755
- changing mode of /usr/local/bin/powerline-lint to 755
- changing mode of /usr/local/bin/powerline-render to 755
- changing mode of /usr/local/bin/powerline-daemon to 755
- Successfully installed powerline-status
- Cleaning up...
第三步:在 Linux 中安裝 Powerline 的字體
Powerline 使用特殊的符號來為開發(fā)者顯示特殊的箭頭效果和符號內(nèi)容。因此你的系統(tǒng)中必須要有符號字體或者補丁過的字體。
通過下面的 wget 命令下載***的系統(tǒng)字體及字體配置文件。
- # 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/
接下來你需要通過如下命令更新你系統(tǒng)的字體緩存。
- # fc-cache -vf /usr/share/fonts/
其次安裝字體配置文件。
- # mv 10-powerline-symbols.conf /etc/fonts/conf.d/
注意:如果相應(yīng)的符號沒有出現(xiàn),可以嘗試關(guān)閉終端會話并重啟 X window,這樣就會生效了。
第四步:給 Bash Shell 和 Vim 狀態(tài)行設(shè)置 Powerline
在這一節(jié)將介紹 bash shell 和 vim 編輯器中關(guān)于 Powerline 的配置。首先通過在 ~/.bashrc 中添加如下內(nèi)容以便設(shè)置終端為 256 色。
- export TERM="screen-256color"
打開 Bash Shell 中的 Powerline
如果希望在 bash shell 中默認打開 Powerline,可以在 ~/.bashrc 中添加如下內(nèi)容。
首先通過如下命令獲取 powerline 的安裝位置。
- # pip show powerline-status
- Name: powerline-status
- Version: 2.2.dev9999-git.aa33599e3fb363ab7f2744ce95b7c6465eef7f08
- Location: /usr/local/lib/python2.7/dist-packages
- Requires:
一旦找到 powerline 的具體位置后,根據(jù)你系統(tǒng)的情況替換到下列行中的 /usr/local/lib/python2.7/dist-packages 對應(yīng)的位置。
- powerline-daemon -q
- POWERLINE_BASH_CONTINUATION=1
- POWERLINE_BASH_SELECT=1
- . /usr/local/lib/python2.7/dist-packages/powerline/bindings/bash/powerline.sh
然后退出后重新登錄,現(xiàn)在 powerline 的狀態(tài)行應(yīng)該如下顯示了。
Bash Powerline Statuslines
現(xiàn)在切換目錄并注意顯示你當(dāng)前路徑的面包屑導(dǎo)航提示的變化。
如果遠程 Linux 服務(wù)器上安裝了 powerline,你能看到后臺掛起的任務(wù),當(dāng)你用 ssh 登錄上去時,會看到該提示增加了主機名。
在 Vim 中打開 Powerline
如果你喜歡使用 vim,正好有一個 vim 的強力插件??梢栽?~/.vimrc 中添加如下內(nèi)容打開該插件(LCTT 譯注:注意同樣需要根據(jù)你的系統(tǒng)情況修改路徑)。
- set rtp+=/usr/local/lib/python2.7/dist-packages/powerline/bindings/vim/
- set laststatus=2
- set t_Co=256
然后你打開 vim 后會看到一個新的狀態(tài)行:
Vim Powerline Statuslines
總結(jié)
Powerline 可以在某些軟件中提供顏色鮮艷、很優(yōu)美的狀態(tài)行及提示內(nèi)容,這對編程環(huán)境有利。希望這篇指南對您有幫助,如果您需要幫助或者有任何好的想法,請留言給我。
作者簡介:
我是Ravi Saive,TecMint的作者。一個喜歡分享訣竅和想法的電腦極客及Linux專家。我的大部分服務(wù)都運行在開源平臺Linux中。關(guān)注我的Twitter,F(xiàn)acebook和Google+。