Jarvis' Blog 总有美丽的风景让人流连

Powerline 设置

2021-03-29
Jarvis
Post

1. 简介

Powerline 是个终端状态线的插件, 可以为 bash, vim, zsh, tmux, ipython 等终端编辑和终端命令工具提供丰富的状态线显示.

2. 安装 powerline-status

参考: Github

  • 在 Ubuntu 20.04 系统下可以使用 pip3 来安装:
1
sudo pip3 install powerline-status
  • 获取文件的安装位置, 与所使用的 python3 版本有关
1
2
powerline_location=$(/usr/bin/pip3 show powerline-status \
    | grep -Po "(?<=Location: ).*(?=$)") && echo $powerline_location
  • 把相关的配置文件复制到用户目录下, 方便修改
1
2
3
4
mkdir -p ~/.config/powerline/colorthemes
mkdir -p ~/.config/powerline/themes/shell

cp $powerline_location/powerline/config_files/config.json ~/.config/powerline/
  • 为终端启用 Powerline

自行把下面的 <powerline_location> 修改为上面获得的路径的值.

把以下内容填入 ~/.bashrc 的末尾:

1
2
3
if [ -f <powerline_location>/powerline/bindings/bash/powerline.sh ]; then
    source <powerline_location>/powerline/bindings/bash/powerline.sh
fi

配置结果如下图所示:

图 1: Bash Powerline

把以下内容填入 ~/.vimrc 的末尾:

1
2
3
4
5
set rtp+=<powerline_location>/powerline/bindings/vim/
" Always show statusline
set laststatus=2
" Use 256 colours (Use this setting only if your terminal supports 256 colours)
set t_Co=256

配置结果如下图所示:

图 2: Vim Powerline

把以下内容填入 ~/.tmux.conf 的末尾:

1
2
source <powerline_location>/powerline/bindings/tmux/powerline.conf
set-option -g default-terminal "screen-256color"

配置结果如下图所示:

图 3: Tmux Powerline

3. 安装并配置 powerline-gitstatus

参考: Github

  • 使用 pip3 安装:
1
sudo pip3 install powerline-gitstatus
  • 创建 ~/.config/powerline/colorthemes/default.json, 填入配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "groups": {
    "gitstatus":                 { "fg": "gray8",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch":          { "fg": "gray8",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch_clean":    { "fg": "green",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch_dirty":    { "fg": "gray8",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch_detached": { "fg": "mediumpurple",    "bg": "gray2", "attrs": [] },
    "gitstatus_tag":             { "fg": "darkcyan",        "bg": "gray2", "attrs": [] },
    "gitstatus_behind":          { "fg": "gray10",          "bg": "gray2", "attrs": [] },
    "gitstatus_ahead":           { "fg": "gray10",          "bg": "gray2", "attrs": [] },
    "gitstatus_staged":          { "fg": "green",           "bg": "gray2", "attrs": [] },
    "gitstatus_unmerged":        { "fg": "brightred",       "bg": "gray2", "attrs": [] },
    "gitstatus_changed":         { "fg": "mediumorange",    "bg": "gray2", "attrs": [] },
    "gitstatus_untracked":       { "fg": "brightestorange", "bg": "gray2", "attrs": [] },
    "gitstatus_stashed":         { "fg": "darkblue",        "bg": "gray2", "attrs": [] },
    "gitstatus:divider":         { "fg": "gray8",           "bg": "gray2", "attrs": [] }
  }
}
  • 创建 ~/.config/powerline/themes/shell/default.json, 填入配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
    "segments": {
        "left": [{
                "function": "powerline.segments.shell.mode"
            },
            {
                "function": "powerline.segments.common.net.hostname",
                "priority": 10
            },
            {
                "function": "powerline.segments.common.env.user",
                "priority": 30
            },
            {
				"function": "powerline.segments.common.env.virtualenv",
				"priority": 30
			},
            {
                "function": "powerline_gitstatus.gitstatus",
                "priority": 10
            },
            {
                "function": "powerline.segments.shell.cwd",
                "priority": 10
            }
        ],
        "right": []
    }   
}
  • 创建 ~/.config/powerline/themes/shell/__main__.json, 填入配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
    "gitstatus": {
        "args": {
            "formats": {
                "branch": "\ue0a0 {}",
                "tag": " ★ {}",
                "behind": " ↓ {}",
                "ahead": " ↑ {}",
                "staged": " ● {}",
                "unmerged": " ✖ {}",
                "changed": " ✚ {}",
                "untracked": " … {}",
                "stashed": " ⚑ {}"
            },
            "show_tag": "exact"
        }
    }   
}
  • 重新载入配置
1
powerline-daemon --replcae

4. 安装 Powerline 字体

  • 可以安装 Powerline 官方提供的补丁字体
1
git clone https://github.com/powerline/fonts.git && cd fonts && sh ./install.sh
  • 也可以安装微软提供的 Cascadia Code 字体, 其中包含适用于 Powerline 的打补丁的版本. 到 Release 页面下载最新的版本, 解压后直接安装TTF. 然后在终端的预设页面把字体修改为 Cascadia Code PL Roman 即可.

上一篇 apt 命令

Content