Powershell比起zsh并不是太好用,但通过适当的配置,可以达到接近nix系统 上终端的效果。
请保证你的操作系统至少是Windows 10,我并没有在更老的操作系统上测试过
最终效果
- 好看的UI,并且每一条命令前能提示git, node相关信息
- Tab 自动补全Git命令 和 路径
- 方向键 上下自动补全输入历史
Windows Terminal
这是一个微软出品的终端,如果你的操作系统是Windows 11,那么它已经被集成在了系统中。
您也可以使用其它终端,请按照对应的使用方法修改字体和启动的Shell。
前往 Windows Terminal - Microsoft Store 应用程序。 点击获取,会弹出
按照提示安装即可。 然后我们打开Windows terminal,它应该是这样的
安装字体
oh-my-posh有一些主题使用了Nerd font,我们需要安装他们避免出现乱码 进入Nerd Fonts - Iconic font aggregator, glyphs/icons collection, & fonts patcher。下载喜欢的字体。我这里选择了Caskaydia Cove Nerd Font。
安装的时候请选择带
Windows Compatible
后缀的字体。
接下来打开我们的Windows Terminal设置项
选择左侧配置文件下的默认值
进入外观选项,将字体改为你刚刚安装的Nerd Font字体
安装Powershell 7,并将它改为Windows terminal 默认Shell
Installing PowerShell on Windows - PowerShell | Microsoft Docs 按照这个页面的提示,安装Powershell 7。
winget install --id Microsoft.Powershell --source winget
接着打开Windows Terminal的设置项
将启动tab下的默认配置文件改为
PowerShell
(请注意,不是Windows Powershell
)
安装oh-my-posh,posh-git和Readline
请运行下列的命令
Install-Module -Name PowerShellGet -Force
winget install JanDeDobbeleer.OhMyPosh -s winget
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force
Install-Module PSReadLine
修改配置文件
接下来,我们需要修改我们Powershell的配置文件。
code $PROFILE
# 如果你没有装vscode,请使用下面的这条命令,将打开记事本
notepad.exe $PROFILE
我们在这个文件中加入下列内容
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete #Tab键会出现自动补全菜单
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
# 上下方向键箭头,搜索历史中进行自动补全
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/jandedobbeleer.omp.json" | Invoke-Expression
Import-Module posh-git # git的自动补全
接着我们打开一个新的Windows terminal窗口,成品应该是这样的
Q & A
为什么在VSCode/Webstorm编辑器的集成终端中出现了乱码
请修改对应编辑器终端的字体为上面安装字体章节的字体
为什么我没有winget
命令
您的系统比较老旧,请参照使用 winget 工具安装和管理应用程序 | Microsoft Docs安装winget。
如何更换oh-my-posh的主题
请执行Get-PoshThemes
,它会展示所有的theme,之后修改$PROFILE
文件中的--config
参数,比如替换掉jandedobbeleer.omp.json
中的jandedobbeleer
为任意主题