閱讀157 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Powerline:給 Vim 和 Bash 提供更棒的狀態行和提示信息

Powerline 是一個極棒的 Vim 編輯器的狀態行插件,這個插件是使用 Python 開發的,主要用於顯示狀態行和提示信息,適用於很多軟件,比如 bash、zsh、tmux 等等。

特色

  1. 使用 python 編寫,使其更具擴展性且功能豐富
  2. 穩定易測的代碼庫,兼容 python 2.6+ 和 python 3
  3. 支持多種 Linux 功能及工具的提示和狀態欄
  4. 通過 JSON 保存配置和顏色方案
  5. 快速、輕量級,具有後台守護進程支持,提供更佳的性能

Powerline 效果截圖

Vim 中 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


  1. # apt-get install python-pip

示例輸出:


  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. Recommended packages:
  5. python-dev-all python-wheel
  6. The following NEW packages will be installed:
  7. python-pip
  8. 0 upgraded, 1 newly installed, 0 to remove and 533 not upgraded.
  9. Need to get 97.2 kB of archives.
  10. After this operation, 477 kB of additional disk space will be used.
  11. Get:1 http://archive.ubuntu.com/ubuntu/ trusty-updates/universe python-pip all 1.5.4-1ubuntu3 [97.2 kB]
  12. Fetched 97.2 kB in 1s (73.0 kB/s)
  13. Selecting previously unselected package python-pip.
  14. (Reading database ... 216258 files and directories currently installed.)
  15. Preparing to unpack .../python-pip_1.5.4-1ubuntu3_all.deb ...
  16. Unpacking python-pip (1.5.4-1ubuntu3) ...
  17. Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
  18. Setting up python-pip (1.5.4-1ubuntu3) ...

在 CentOS、RHEL 和 Fedora 中安裝 pip

在 Fedora 類係統中,需要先打開 epel 倉庫,然後按照如下方法安裝 pip 包。


  1. # yum install python-pip
  2. # dnf install python-pip [Fedora 22+ 以上]

示例輸出:


  1. Installing:
  2. python-pip noarch 7.1.0-1.el7 epel 1.5 M
  3. Transaction Summary
  4. =================================================================================
  5. Install 1 Package
  6. Total download size: 1.5 M
  7. Installed size: 6.6 M
  8. Is this ok [y/d/N]: y
  9. Downloading packages:
  10. python-pip-7.1.0-1.el7.noarch.rpm | 1.5 MB 00:00:01
  11. Running transaction check
  12. Running transaction test
  13. Transaction test succeeded
  14. Running transaction
  15. Installing : python-pip-7.1.0-1.el7.noarch 1/1
  16. Verifying : python-pip-7.1.0-1.el7.noarch 1/1
  17. Installed:
  18. python-pip.noarch 0:7.1.0-1.el7
  19. Complete!

第二步:在 Linux 中安裝 Powerline

現在可以從 Git 倉庫中安裝 Powerline 的最新開發版。在此之前係統需要安裝好 Git 工具以便可以從倉庫拉下代碼。


  1. # apt-get install git
  2. # yum install git
  3. # dnf install git

然後你可以通過 pip 命令安裝 Powerline。


  1. # pip install git+git://github.com/powerline/powerline

示例輸出:


  1. Cloning git://github.com/powerline/powerline to /tmp/pip-WAlznH-build
  2. Running setup.py (path:/tmp/pip-WAlznH-build/setup.py) egg_info for package from git+git://github.com/Lokaltog/powerline
  3. warning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'
  4. warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'
  5. Installing collected packages: powerline-status
  6. Found existing installation: powerline-status 2.2
  7. Uninstalling powerline-status:
  8. Successfully uninstalled powerline-status
  9. Running setup.py install for powerline-status
  10. warning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'
  11. warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'
  12. changing mode of build/scripts-2.7/powerline-lint from 644 to 755
  13. changing mode of build/scripts-2.7/powerline-daemon from 644 to 755
  14. changing mode of build/scripts-2.7/powerline-render from 644 to 755
  15. changing mode of build/scripts-2.7/powerline-config from 644 to 755
  16. changing mode of /usr/local/bin/powerline-config to 755
  17. changing mode of /usr/local/bin/powerline-lint to 755
  18. changing mode of /usr/local/bin/powerline-render to 755
  19. changing mode of /usr/local/bin/powerline-daemon to 755
  20. Successfully installed powerline-status
  21. Cleaning up...

第三步:在 Linux 中安裝 Powerline 的字體

Powerline 使用特殊的符號來為開發者顯示特殊的箭頭效果和符號內容。因此你的係統中必須要有符號字體或者補丁過的字體。

通過下麵的 wget 命令下載最新的係統字體及字體配置文件。


  1. # wget https://github.com/powerline/powerline/raw/develop/font/PowerlineSymbols.otf
  2. # wget https://github.com/powerline/powerline/raw/develop/font/10-powerline-symbols.conf

然後你將下載的字體放到字體目錄下 /usr/share/fonts 或者 /usr/local/share/fonts,或者你可以通過 xset q 命令找到一個有效的字體目錄。


  1. # mv PowerlineSymbols.otf /usr/share/fonts/

接下來你需要通過如下命令更新你係統的字體緩存。


  1. # fc-cache -vf /usr/share/fonts/

其次安裝字體配置文件。


  1. # mv 10-powerline-symbols.conf /etc/fonts/conf.d/

注意:如果相應的符號沒有出現,可以嚐試關閉終端會話並重啟 X window,這樣就會生效了。

第四步:給 Bash Shell 和 Vim 狀態行設置 Powerline

在這一節將介紹 bash shell 和 vim 編輯器中關於 Powerline 的配置。首先通過在 ~/.bashrc 中添加如下內容以便設置終端為 256 色。


  1. export TERM="screen-256color"

打開 Bash Shell 中的 Powerline

如果希望在 bash shell 中默認打開 Powerline,可以在 ~/.bashrc 中添加如下內容。

首先通過如下命令獲取 powerline 的安裝位置。


  1. # pip show powerline-status
  2. Name: powerline-status
  3. Version: 2.2.dev9999-git.aa33599e3fb363ab7f2744ce95b7c6465eef7f08
  4. Location: /usr/local/lib/python2.7/dist-packages
  5. Requires:

一旦找到 powerline 的具體位置後,根據你係統的情況替換到下列行中的/usr/local/lib/python2.7/dist-packages 對應的位置。


  1. powerline-daemon -q
  2. POWERLINE_BASH_CONTINUATION=1
  3. POWERLINE_BASH_SELECT=1
  4. . /usr/local/lib/python2.7/dist-packages/powerline/bindings/bash/powerline.sh

然後退出後重新登錄,現在 powerline 的狀態行應該如下顯示了。

Bash Powerline Statuslines

Bash Powerline Statuslines

現在切換目錄並注意顯示你當前路徑的麵包屑導航提示的變化。

如果遠程 Linux 服務器上安裝了 powerline,你能看到後台掛起的任務,當你用 ssh 登錄上去時,會看到該提示增加了主機名。

在 Vim 中打開 Powerline

如果你喜歡使用 vim,正好有一個 vim 的強力插件。可以在 ~/.vimrc 中添加如下內容打開該插件(LCTT 譯注:注意同樣需要根據你的係統情況修改路徑)。


  1. set rtp+=/usr/local/lib/python2.7/dist-packages/powerline/bindings/vim/
  2. set laststatus=2
  3. set t_Co=256

然後你打開 vim 後會看到一個新的狀態行:

Vim Powerline Statuslines

原文發布時間為:2017-01-16

本文來自雲棲社區合作夥伴“Linux中國”

最後更新:2017-05-27 10:32:34

  上一篇:go  微軟選擇 Ubuntu 放到 Windows 10 的 Linux 子係統中,或許“錯”了
  下一篇:go  UE4: 學習虛幻引擎4的16條準則