WSL 2 and Windows Terminal Experience

Warning! 早期版本,Microsoft 随时会改

看过我其他文章的同学应该都知道,我是不用 Windows 的.今天突然心血来潮体验了下 WSL2Windows Terminal, 发现 WSL2 有进步, 不像 WSL1 那样 apt update 都得等半天. Terminal 也很爽. 字体主题都能配置.
为了方便后来的同学,简单贴一下

1. 安装WSL2

确认系统版本

请确定你的 Windows 版本高于 18917 ,属于 20H1 分支,运行winver就可以看到版本号,现在还在 Preview 通道.没有的话可以在 UUP @rgadguardUUP dump 下载UUP制作最新的预览版镜像.

启用虚拟机平台可选组件

首先在 Windows 程序和功能里勾选安装 Linux 子系统功能
然后 PowerShell 管理员运行:

1
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

然后会自动重新启动计算机.

设置WSL版本为WSL 2

修改发行版为 WSL2 需要在 PowerShell 中运行:

1
2
3
4
5
6
7
8
9
10
11
// Setting the Distro to WSL2
wsl --set-version <Distro> 2

// Example: Debian
// ~$ wsl -l
// Windows Subsystem for Linux Distributions:
// Debian (Default)
wsl --set-version Debian 2

// Restore to WSL 1
wsl --set-version Debian 1

新安装的发行版默认使用WSL2:

1
wsl --set-default-version 2

查看各个发行版的WSL版本:

1
2
3
wsl --list --verbose

wsl -l -v

2. 安装 Microsoft Terminal

官方仓库的 Release 已经有编译好的版本了, 下载安装就行, 写文章时的版本是 v0.2.1831.0.

3. 配置国内镜像源

1
2
3
4
sudo cp /etc/apt/sources.list /etc/apt/sources.list.$(date +%Y%m%d%H%M%S)
sudo sed -i "s/deb.debian.org/mirrors.huaweicloud.com/g" /etc/apt/sources.list
sudo sed -i "s/ftp.debian.org/mirrors.huaweicloud.com/g" /etc/apt/sources.list
sudo sed -i "s/security.debian.org/mirrors.huaweicloud.com/g" /etc/apt/sources.list

3. zsh 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
sudo apt update && sudo apt install ca-certificates curl git zsh
# install oh-my-zsh
mkdir -p ~/.zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

# remove robbyrussell
sed -i "s/ZSH_THEME=\"robbyrussell\"//" ~/.zshrc

# set powerlevel9k.zsh
sed -i '1isource ~/.zsh/powerlevel9k.zsh' ~/.zshrc

# install zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
sed -i '1isource ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh' ~/.zshrc

~/.zsh/powerlevel9k.zsh 内容:

1
2
3
4
5
6
7
ZSH_THEME="powerlevel9k/powerlevel9k"
# Left
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir dir_writable vcs)
# Right
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status background_jobs history ram load time)
# for nerd-font
POWERLEVEL9K_MODE="nerdfont-complete"

我觉得 powerlevel9k 配 nerd 字体包是很舒服的,WSL 终端的字体效果不好,我稍微配置了下,还不如 cmder. 新终端完美支持了 nerdfont, 解决了Windows 一大痛点.
我用的字体是DejaVuSansMono Nerd Font, 在Releases · ryanoasis_nerd-fonts 下载, 双击安装. 可以只下载 DejaVuSansMono.zip, 你想体验其他字体也可以直接下载. 现在 source ~/.zshrc以后,字体应该还是默认的, 请看完第四部分

4. Terminal 设置

在 Terminal 使用快捷键 Ctrl+` 打开设置,用VS Code、记事本之类的文本编辑器打开,搜索 wsl.exe 可以找到 wsl 的部分

1
2
3
4
5
6
"defaultProfile" : "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
// for wsl
"colorScheme" : "Calamity",
"fontFace" : "DejaVuSansMono Nerd Font",
"guid" : "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
"startingDirectory" : "//wsl$/Debian/home/my/",
  • defaultProfile 用来设置默认启动的命令行, wsl/powershell/cmd, 用 guid 来设置

    1
    2
    3
    4
    // default value is powershell's guid
    "defaultProfile" : "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
    // wsl's guid
    "guid" : "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
  • 自带的 schemes 不多: “Campbell”/“One Half Dark”/“One Half Light”/“Solarized Dark”/“Solarized Light” 我看惯了Calamity, 就自己加了个.
    iTerm2-Color-Schemes/windowsterminal 里也有其他的, 好像是之前的 schemes 都给 Windows Terminal 做了吧,我没仔细看。

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
// for WSL
"colorScheme" : "Calamity",
// for schemes
"schemes" :
[
{
"name": "Calamity",
"black": "#2f2833",
"red": "#fc644d",
"green": "#a5f69c",
"yellow": "#e9d7a5",
"blue": "#3b79c7",
"purple": "#f92672",
"cyan": "#74d3de",
"white": "#d5ced9",
"brightBlack": "#7e6c88",
"brightRed": "#fc644d",
"brightGreen": "#a5f69c",
"brightYellow": "#e9d7a5",
"brightBlue": "#3b79c7",
"brightPurple": "#f92672",
"brightCyan": "#74d3de",
"brightWhite": "#ffffff",
"background": "#2f2833",
"foreground": "#d5ced9"
},
  • 自定义字体, 如果不知道怎么写, 可以在设置里搜字体, 抄设置里的.
1
"fontFace" : "DejaVuSansMono Nerd Font",

  • 设置启动时在你 WSL的家目录 ~/ , 而不是Windows的家目录 /mnt/c/Users/${username}
"startingDirectory" : "//wsl$/your_distro/home/your_username/",

差不多就是这些, 感觉微软努努力还是能上台面的

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×