在Windows Terminal中使用zsh

在Linux上使用oh-my-zsh非常爽,而windows下cmd界面太丑陋了。但PowerShell却可以扩展,同样能达到类似Linux下的效果。

一、安装

Windows自带了PowerShell,默认版本为5.x。如要要使用更高的版本7.x,可以参考:PowerShell 文档 - PowerShell | Microsoft Docs

下面的操作都是在powershell下面进行。

1.安装oh-my-posh和posh-git

类似于oh-my-zsh,oh-my-posh 为 PowerShell 提供了很多自定义主题和配色,而 posh-git 为 PowerShell 提供了 git 状态显示和命令补全等。

Install-Module posh-git -Scope CurrentUser 
Install-Module oh-my-posh -Scope CurrentUser

2.安装PSReadLine

Windows自带的PowerShell默认版本是5.x,需要安装PSReadLine。如果使用 PowerShell 7.x以上版本,自带了 PSReadLine,不需要手动安装。

# windows默认PowerShwll为版本为5.x,需要安装PSReadLine
Install-Module PSReadLine

3.配置

生成配置文件。使用以下命令生成配置文件。Windows自带的WindowsShell配置在C:Users用户名DocumentsWindowsPowerShell下,7.x在同级目录下,但文件夹名称为PowerShell。

# 如果之前没有配置文件,就新建一个 PowerShell 配置文件
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }

用记事本打开配置文件

notepad $PROFILE

写入以下内容。

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

Set-PoshPrompt Paradox # 设置主题为 Paradox
Set-PSReadLineOption -PredictionSource History # 设置预测文本来源为历史记录
Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录

当然,这里主题、快捷键等都是可以自定义的。设置好后保存,重启 PowerShell。

4.安装字体

安装支持PowerShell环境下的字体。

5.在PowerShell窗口中配置字体

在PowerShell窗口的标题栏上右键,属性->字体,选择字体。选择字体名称中含有PL或者Powerline 的字体,这些字体是专门为PowerShell环境设计的字体。

看看效果吧。

二、与Windows Terminal集成

Windows Terminal默认已经将PowerShell集成进来了。如果额外安装了PowerShell 7.x的版本,也可以将其继承进来。

直接使用msi的安装包来进行安装,不用任何设置默认就被集成进来了。

在Window Terminal的设置中,通过图形界面来配置字体,或者也可以直接在json配置文件中配置。

    {
        "commandline": "powershell.exe", 
        "fontFace": "Cascadia Code PL",
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "hidden": false,
        "name": "Windows PowerShell"
    },
    {
        "commandline": "pwsh.exe",
        "fontFace": "Cascadia Code PL",
        "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
        "hidden": false,
        "name": "PowerShell 7",
        "source": "Windows.Terminal.PowershellCore"
    },

注意:

默认不是管理员权限运行。如果将启动文件设置了  兼容性->勾选 "以管理员身份运行此程序",则在Windows Ternimal中无法打开

不积跬步,无以至千里。不积小流,无以成江海!
原文地址:https://www.cnblogs.com/rouqinglangzi/p/10482623.html