从Cmder到Windows Terminal

从Cmder到Windows Terminal

  记录一下从Cmder迁移至Windows Terminal,并进行一些优化的过程,果然人还是喜欢没事折腾一下的。

卸载Cmder

  第一步当然是干掉Cmder咯,软件虽然是解压即用的,在删除之前还是要完成以下几步,防止残留。

删除右键菜单

  首先把右键的“Cmder Here”去掉,新建文本文件,键入如下内容并改为reg文件执行:

1
2
3
4
Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\Directory\Background\shell\cmder]
[-HKEY_CLASSES_ROOT\Directory\shell\cmder]

取消ConEmu

  为了避免命令提示符路径错误,需要取消Cmder中的强制替换。在Settings→Integration→Default term中取消勾选Force ConEmu as default terminal for console applications。

删除环境变量

  将之前配置好的环境变量删除,这里不必赘述了。

Windows Terminal安装与配置

下载安装

  在Microsoft Store搜索Windows Terminal,下载安装即可。

安装字体

  看到参考资料有推荐程序员字体 :Fira Code,感觉确实不错,遂采用。访问地址下载,解压ttf文件夹,然后全选安装。

安装PowerShell 7

  启动Window Terminal会提示尝试新的跨平台 PowerShell https://aka.ms/pscore6,看着很不爽,访问其Github发布页,下载指定版本,一般为PowerShell-7.x.x-win-x64.msi。

安装PowerShell插件

  打开PowerShell,逐行输入如下命令:

1
2
3
4
5
6
7
8
# 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh
Install-Module -Name PSReadLine -AllowPrerelease -Force

# 2. 安装 posh-git 包,让你的 git 更好用
Install-Module posh-git -Scope CurrentUser

# 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅
Install-Module oh-my-posh -Scope CurrentUser

  如果提示来源可能不受系统信任,输入 Y 信任即可。

配置

默认配置

  将defaultProfile设置为PowerShell的GUID:"{574e775e-4f2a-5b96-ac1e-a2962a402336}"

配色

  可以在Terminal Color Scheme Designer选择喜欢的主题,导出为Alacritty格式复制粘贴即可,也可以在Windows Terminal Themes中挑选,原理同上。

  这里俺偷了老弟@Kotori-y的配色,稍加改动,配置如下:

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
// 此处为defaults设置
{
"startingDirectory": ".",
"fontFace": "Fira Code",
"fontSize": 11,
"historySize": 9001,
"padding": "5, 5, 20, 25",
"snapOnInput": true,
"useAcrylic": false,
"acrylicOpacity": 0.55,
"backgroundImage" : "%LOCALAPPDATA%\\packages\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\RoamingState\\Reimu.jpg",
"backgroundImageOpacity" : 0.3,
"colorScheme": "Seafoam Pastel"
}

// 此处为schemes设置
{
"name": "Seafoam Pastel",
"background": "#243435",
"foreground": "#d4e7d4",
"selectionBackground": "#B00C11",

//ANSI color
"black": "#757575",
"red": "#825d4d",
"green": "#728c62",
"yellow": "#ada16d",
"blue": "#4d7b82",
"purple": "#8a7267",
"cyan": "#729494",
"white": "#e0e0e0",
"brightBlack": "#8a8a8a",
"brightRed": "#cf937a",
"brightGreen": "#98d9aa",
"brightYellow": "#fae79d",
"brightBlue": "#7ac3cf",
"brightPurple": "#d6b2a1",
"brightCyan": "#ade0e0",
"brightWhite": "#e0e0e0"
}

  其中背景图片需要自行改动设置。

下拉列表

  根据个人需求稍加改动了一些,配置如下:

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
{
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"name": "PowerShell",
"source": "Windows.Terminal.PowershellCore",
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "CMD",
"commandline": "cmd.exe",
"hidden": false
},
{
"guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
"hidden": false,
"name": "Ubuntu-18.04",
"source": "Windows.Terminal.Wsl"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": true,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"name": "PowerShell",
"source": "Windows.Terminal.PowershellCore"
},
{
"guid": "{ee4fe116-1375-4c00-925c-1e361f99496d}",
"name": "IPython",
"commandline": "cmd.exe /C D:\\Users\\Catkin\\Anaconda3\\Scripts\\activate.bat D:\\Users\\Catkin\\Anaconda3 & ipython",
"hidden": false,
"icon": "C:\\Users\\Catkin\\AppData\\Local\\Packages\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\RoamingState\\ipython_nb.ico"
}

  其中IPython的路径和icon需要自行改动设置。

添加右键菜单

  使用脚本自动生成右键菜单。

  将上述仓库的脚本Clone下来,然后管理员模式运行PowerShell 7,切换至src目录,执行:

1
.\install.ps1 Default

  也可以用参数mini实现迷你模式;也有展开模式,可用参数Flat实现。

  如要卸载,则执行如下代码:

1
.\uninstall.ps1 Default

  俺稍加改动了一下,主要是感觉太长了,如有需要亦可自行修改。

添加启动参数

  在 powershell 中输入

1
notepad.exe $Profile

  在编辑如下代码,保存关闭。

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
#------------------------------- Import Modules BEGIN -------------------------------
# 引入 posh-git
Import-Module posh-git

# 引入 oh-my-posh
Import-Module oh-my-posh

# 设置 PowerShell 主题
Set-Theme Paradox
#------------------------------- Import Modules END -------------------------------





#------------------------------- Set Hot-keys BEGIN -------------------------------
# 设置 Tab 键补全
# Set-PSReadlineKeyHandler -Key Tab -Function Complete

# 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#------------------------------- Set Hot-keys END -------------------------------





#------------------------------- Functions BEGIN -------------------------------
# Python 直接执行
$env:PATHEXT += ";.py"

# 更新 pip 的方法
function Update-Packages {
# update pip
Write-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan
$a = pip list --outdated
$num_package = $a.Length - 2
for ($i = 0; $i -lt $num_package; $i++) {
$tmp = ($a[2 + $i].Split(" "))[0]
pip install -U $tmp
}

# update TeX Live
$CurrentYear = Get-Date -Format yyyy
Write-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyan
tlmgr update --self
tlmgr update --all
}
#------------------------------- Functions END -------------------------------





#------------------------------- Set Alias Begin -------------------------------
# 1. 编译函数 make
function MakeThings {
nmake.exe $args -nologo
}
Set-Alias -Name make -Value MakeThings

# 2. 更新系统 os-update
Set-Alias -Name os-update -Value Update-Packages

# 3. 查看目录 ls & ll
function ListDirectory {
(Get-ChildItem).Name
Write-Host("")
}
Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem
#------------------------------- Set Alias END -------------------------------

参考

Kotori-y的个人配置

Windows Terminal 完美配置 PowerShell 7.1

评论