目錄

Powerline for WSL Ubuntu

目錄

Powerline 真是一個設定一次忘記一次的東西, 先把 bsah shell 用的設定寫下來好了,下次再來記錄 fish shell 用的設定。

因為 Powershell 用到一些特殊字元必須使用 patch font,所以要先確定自己的環境有支援這樣的字型。 要注意的是,Windows 電腦本身以及 WSL 內的 Ubuntu 都必須安裝喔。

在 shell 安裝

1
2
3
4
5
6
7
8
sudo apt update
sudo apt upgrade -y
cd ~
git clone https://github.com/powerline/fonts.git
cd fonts
./install.sh
cd ..
rm -rf fonts

在 Windows 用 powershell 安裝(記得用管理者權限開啟 powershell)

1
2
3
4
5
git clone https://github.com/powerline/fonts.git
cd fonts
Set-ExecutionPolicy Bypass -Scope Process -Force; ./install.ps1
cd ..
rd fonts -Recurse -Force

安裝完畢後,先來修改 Windows Terminal 的設定檔來使用指定的字型,我習慣用 Source Code Pro 所以這邊的範例會採用 Source Code Pro for Powerline

開啟 Windows Terminal 後,點選下拉選單後按設定後可以開啟設定文件檔,該文件檔為 json 格式。

31行是用來修改字型,32行是修改顏色布局。

 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// This file was initially generated by Windows Terminal 1.0.1401.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",

    // You can add more global application settings here.
    // To learn more about global settings, visit https://aka.ms/terminal-global-settings

    // If enabled, selections are automatically copied to your clipboard.
    "copyOnSelect": true,

    // If enabled, formatted data is also copied to your clipboard
    "copyFormatting": false,

    // A profile specifies a command to execute paired with information about how it should look and feel.
    // Each one of them will appear in the 'New Tab' dropdown,
    //   and can be invoked from the commandline with `wt.exe -p xxx`
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
            "fontFace": "Source Code Pro for Powerline",
            "colorScheme": "Solarized Dark"
        },
        "list":
        [
            {
                // Make changes here to the powershell.exe profile.
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                "hidden": false
            },
            {
                // Make changes here to the cmd.exe profile.
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "命令提示字元",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
                "hidden": false,
                "name": "Ubuntu-20.04",
                "source": "Windows.Terminal.Wsl"
            },
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": false,
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl"
            }
        ]
    },

    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [],

    // Add custom keybindings to this array.
    // To unbind a key combination from your defaults.json, set the command to "unbound".
    // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
    "keybindings":
    [
        // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
        // These two lines additionally bind them to Ctrl+C and Ctrl+V.
        // To learn more about selection, visit https://aka.ms/terminal-selection
        { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
        { "command": "paste", "keys": "ctrl+v" },

        // Press Ctrl+Shift+F to open the search box
        { "command": "find", "keys": "ctrl+shift+f" },

        // Press Alt+Shift+D to open a new pane.
        // - "split": "auto" makes this pane open in the direction that provides the most surface area.
        // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
        // To learn more about panes, visit https://aka.ms/terminal-panes
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
    ]
}

到這邊字型環境就算設定完畢。接下來可以進入 WSL 中的 Ubuntu 來安裝 Powerline 了

1
2
sudo apt install -y build-essential socat python3 python3-pip git fonts-powerline powerline
pip3 install psutil pygit2

接著來修改 .bashrc 使 Powerline 可以在一登入時就啟用。在 .bashrc 內加上下面這幾行

1
2
3
4
5
6
7
8
cd ~
POWERLINE_SCRIPT=/usr/share/powerline/bindings/bash/powerline.sh
if [ -f $POWERLINE_SCRIPT ]; then
  powerline-daemon -q
  POWERLINE_BASH_CONTINUATION=1
  POWERLINE_BASH_SELECT=1
  source $POWERLINE_SCRIPT
fi
提示

上面第2行的指令可能路徑會有所不同,造成出現類似下面這樣的錯誤訊息

1
-bash: /usr/share/powerline/bindings/bash/powerline.sh: No such file or directory

這時可以試著用 find 指令來找路徑

例如

1
find /usr /home -name powerline.sh

這樣應該可以找到正確的路徑

到這邊 bash 的設定就完成了,重新啟動一個 WSL Ubuntu 試試看是不是成功了呢。