5

Windows Terminal 捣鼓

 2 years ago
source link: https://xienaoban.github.io/posts/3549.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

安装: Win10 商店.

添加 Git Bash 支持

打开设置, 在 profiles -> list 添加以下必须项:

{
"guid": "{c169f907-832c-4aa4-9ab3-84a4658b4b93}",
"name" : "Git Bash",
"commandline" : "C:\\Program Files\\Git\\bin\\bash.exe -i -l",
"icon" : "C:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico"
}

其他的配置看自己喜好就行, 我都定义在 defaults 里了, 所以这里只有四项.

guid 其实随便填一个与其他 shell 不同的序列号即可, 这里我是用 Visual Studio 生成了一个.

commandline 定位到 Git Bash 安装路径下的 bin 文件夹. 注意要带上 -i -l 的参数. 参数的作用: 如果不加, 则不会加载 Git Bash 的配置, 会打开一个没有自定义过的 Bash. 事实上, Git 自带的 Bash Terminal 启动时带的参数就是这个:

个人配置喜好

在打开设置时按住 Alt, 会打开 defaults.json, 里面有所有设置的默认值, 可以拿来作参考.

以下是我的 settings.jsonprofiles 部分:

"profiles":
{
"defaults":
{
"startingDirectory": ".",
"closeOnExit": "graceful",
"historySize": 9001,
"snapOnInput": true,
"colorScheme": "One Half Dark",
"fontFace": "Cascadia Mono",
"fontSize": 12,
"antialiasingMode": "grayscale",
"useAcrylic": true,
"acrylicOpacity" : 0.9,
"padding": "8, 8, 8, 8",
"cursorShape": "bar"
},
"list":
[
{
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe"
},
{
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "命令提示符",
"commandline": "cmd.exe"
},
{
"guid": "{c169f907-832c-4aa4-9ab3-84a4658b4b93}",
"name" : "Git Bash",
"commandline" : "C:\\Program Files\\Git\\bin\\bash.exe -i -l",
"icon" : "C:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
}
]
},

Windows Terminal 添加到右键菜单

要求效果类似 Git Bash Here, 在此处打开 Powershell 窗口(s), 通过 Code 打开 这种, 在文件夹空白处右键出现菜单:

原理就仿照上面那几位, 创建类似效果的注册表. 上面几个软件的菜单都能在注册表编辑器的如下位置找到:

计算机\HKEY_CLASSES_ROOT\Directory\Background\shell

新建一个 .reg 文件, 写入如下内容:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Windows Terminal here"
"Icon"="<path-to-your-ico>"
"Extended"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\<user>\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"
  • 第 5 行 "Icon" 是图标的路径, 图标随意, 放的路径也随意. 如果不需要图标就删掉这行.

    如果要 Windows Terminal 官方图标, 在他们的 Github 上有: https://github.com/microsoft/terminal/blob/master/res/terminal.ico.

    当然, 上面这个链接只是 Github 上该文件的页面, 转换成源文件链接就是 https://raw.githubusercontent.com/microsoft/terminal/master/res/terminal.ico, 点击即下.

  • 第 6 行 "Extended" 代表需要按住 Shift 才能显示, 就像 Powershell 一样, 删掉该行则不需要按 Shift 也能显示.

  • 第 9 行是 Windows Terminal 的绝对路径 (记得改 <user>), 必须是完全的绝对路径, 不可以是

    %USERPROFILE%\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe
    %LOCALAPPDATA%\\Microsoft\\WindowsApps\\wt.exe

    原因不明, 总之如果不填写完整的绝对路径, 它就报错.

    另外有人的 Windows Terminal 名字是 wtd.exe 而不是 wt.exe, 先去目录确认一下.

我自己用的 right-click menu.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Windows Terminal here"
"Icon"="%LOCALAPPDATA%\\WindowsTerminal\\terminal.ico"

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

注册后右键的效果上面已经展示了.

然后还有问题, 右键打开后 Windows Terminal 路径总是为 C:\Users\<user>. 解决方法有两种:

  1. Windows Terminal 默认启动路径一直是 user profile. 所以打开其 settings.json"startingDirectory": "%USERPROFILE%" 改为 "startingDirectory": ".".

    这个方法比较推荐, 但也有缺点: 改了 startingDirectory 之后, 从开始菜单直接打开 Windows Terminal 会显示路径为 C:\WINDOWS\system32.

  2. 不改 startingDirectory 其实也可以. wt.exe 有个参数 -d 用于设定目录. 所以可以把 .reg 最后一行的 command 改为:

    @="C:\\Users\\xie\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe -d ."

    于是不用设置 startingDirectory, 打开也是当前目录. 但缺点也很明显, 它只限定打开的第一个 terminal 的目录, 如果你按加号新建一个 terminal, 目录就又变成 C:\Users\<user> 了.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK