PowerShell Core美化

前置条件

通过文档我们可以发现需要执行如下命令来安装模块,以管理员身份打开PowerShell, 执行

Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))
Install-Module posh-git

配置profile
执行 $profile 然后会显示profile文件所在路径
一般是%UserProfile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
执行code $profile或者code-insiders $profile用vsc打开或者执行notepad $PROFILE使用系统自带的记事本, 如果没有就会自动创建
内容如下:

Import-Module posh-git
# 这里的config需要自己创建
oh-my-posh init pwsh --config 'C:\Users\你的名字\omp.json' | Invoke-Expression

Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    param($commandName, $wordToComplete, $cursorPosition)
        dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
            [CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
}
Set-PSReadLineKeyHandler -Key "Ctrl+f" -Function ForwardWord
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
Set-PSReadLineOption -PredictionSource History -PredictionViewStyle ListView

完成后默认打开是没有任何效果的,所以我们现在去配置omp.json文件.这里我们以material主题为例,其他主题可以参考他们的官网点击对应主题名称即可进入GitHub中复制配置.

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "alignment": "left",
      "segments": [
        {
          "foreground": "#ffffff",
          "style": "plain",
          "template": "<#C591E8>\u276f</><#69FF94>\u276f</>",
          "type": "text"
        },
        {
          "foreground": "#56B6C2",
          "properties": {
            "style": "folder"
          },
          "style": "plain",
          "template": " {{ .Path }} ",
          "type": "path"
        },
        {
          "foreground": "#D0666F",
          "properties": {
            "branch_icon": "",
            "fetch_status": false
          },
          "style": "plain",
          "template": "<#5FAAE8>git:(</>{{ .HEAD }}<#5FAAE8>)</>",
          "type": "git"
        },
        {
          "foreground": "#DCB977",
          "style": "plain",
          "template": " \uf119",
          "type": "exit"
        },
        {
          "foreground": "#66F68F",
          "properties": {
            "time_format": "15:04"
          },
          "style": "plain",
          "template": " {{ .CurrentDate | date .Format }} ",
          "type": "time"
        }
      ],
      "type": "prompt"
    }
  ],
  "version": 2
}
  • 完成后,重新打开Windows Terminal即可体验漂亮的终端了.

效果如下,按上下键切换 自动提示列表来源于历史记录

posted @ 2022-01-10 17:02  Joe_du  阅读(333)  评论(0编辑  收藏  举报