安装 Scoop

Windows 下的包管理器:Scoop

安装 terminal 使用的字体,推荐使用 JetBrainsMono-NF。

sudo scoop install -k JetBrainsMono-NF

安装 Powershell 7

scoop install pwsh -k

安装并配置 on-my-posh

PowerShell 7 自带 PSReadLine,在 PowerShell 5 上需要自己安装,可以通过 $PSVersionTable 命令查看当前 PowerShell 版本。

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.7
PSEdition                      Core
GitCommitId                    7.3.7
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
  • 以管理员身份运行 PowerShell,安装插件
set-executionpolicy remotesigned
Install-Module PSReadLine -Force
Install-Module posh-git
Install-Module Terminal-Icons
  • 安装 on-my-posh
scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json
  • 下载 oh-my-posh 主题(可选)
git clone https://github.com/JanDeDobbeleer/oh-my-posh

保留其中的 themes 文件夹即可。

配置 PowerShell

notepad $profile

写入以下文本。注意修改 config 路径。

# oh-my-posh init pwsh --config D:\Env\oh-my-posh\themes\my\sail.omp.json | Invoke-Expression
# Import-Module posh-git
# Import-Module Terminal-Icons
# Import-Module scoop-completion

# Shows navigable menu of all options when hitting Tab
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
# 设置向上键为后向搜索历史记录
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
# 设置向下键为前向搜索历史纪录
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward


# 设置预测文本来源为历史记录
# Set-PSReadLineOption -PredictionSource History
# ListView
Set-PSReadLineOption -PredictionSource History -PredictionViewStyle ListView
# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd


# 打开当前工作目录
function OpenCurrentFolder {
    param
    (
        # 输入要打开的路径
        # 用法示例:open C:\
        # 默认路径:当前工作文件夹
        $Path = '.'
    )
    Invoke-Item $Path
}
Set-Alias -Name open -Value OpenCurrentFolder

重新打开 terminal 查看。

配置 Windows Terminal

修改配置文件 settings.json,具体文件见附件。

注意修改 backgroundImage 路径

右键在此处打开 Terminal

将以下文本保存为 .reg 文件并执行,或下载附件执行。

注意:需要下载 terminal.ico 图标文件(见附件),如果不生效试试把 wt 改成 windows terminal 完整路径。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\directory\background\shell\wt]
@="Open Terminal"
"icon"="%LOCALAPPDATA%\\Microsoft\\WindowsApps\\terminal.ico"

[HKEY_CLASSES_ROOT\directory\background\shell\wt\command]
@="wt -d ."

参考

[1] 我的windows terminal美化方案(oh-my-posh3)

[2] 魔改的一个posh3主题

[3] Win11下安装PSReadLine终端自动补全