添加 Windows Terminal 至右键菜单

Windows Terminal 已经在微软应用商店上架了,所以我把终端从 Fluent Terminal 换成了 Windows Terminal,记录一下将它添加到右键菜单的过程

Windows 11 现在已经在右键菜单里默认添加了在终端中打开,所以不需要再单独添加了,已经添加过的去注册表里删掉[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]就好了

添加图标

首先在C:\Users\[YOUR_USER_NAME]\AppData\Local新建文件夹Terminal,可以直接使用下面的命令创建,记得把[YOUR_USER_NAME]换成自己的用户名!

1
mkdir C:\Users\[YOUR_USER_NAME]\AppData\Local\Terminal

然后下载 Windows Terminal 图标到刚才创建的文件夹,如果浏览器没有自动下载的话就右键另存为terminal.ico

terminal.ico 下载

注册表写入

新建一个txt文件,添加下面的内容,然后把文件后缀改为.reg记得把[YOUR_USER_NAME]换成自己的用户名!

1
2
3
4
5
6
7
8
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Windows terminal here"
"Icon"="C:\\Users\\[YOUR_USER_NAME]\\AppData\\Local\\Terminal\\terminal.ico"

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\[YOUR_USER_NAME]\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

完成后双击文件,合并到注册表中

修改配置

这个时候右键菜单中已经出现了Windows terminal here的选项,但是点击后所在的目录并不是当前的目录,这就需要修改Windows Terminal的配置文件了

打开Windows Terminal,点击顶部的下拉菜单,打开设置,此时会打开名为settings.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
30
31
32
33
34
// 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.
},
"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"
}
]
},

要想在当前文件夹启动Windows Terminal,需要在每一段的下面添加"startingDirectory": "./",修改完成后是这个亚子

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
// 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.
},
"list":
[
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false,
"startingDirectory": "./"
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "命令提示符",
"commandline": "cmd.exe",
"hidden": false,
"startingDirectory": "./"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure",
"startingDirectory": "./"
}
]
},

保存后就能够使用右键菜单在当前文件夹运行Windows Terminal

踩坑记录

在使用Powershell的时候可能会出现报错无法加载文件balabala.ps1,因为在此系统上禁止执行脚本,有关详细信息,请参阅balabala,这是因为默认的ExecutionPolicyRestricted,即不允许执行任何脚本。解决方法是以管理员身份运行,并输入命令set-executionpolicy remotesigned即可解除限制

参考:

  1. https://github.com/microsoft/terminal/issues/1060#issuecomment-497539461
  2. https://blog.csdn.net/Jioho_chen/article/details/101159291