一、准备工作

首先需要安装Chocolatey:

  1. Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

关闭脚本禁用:允许Powershell运行脚本:

  1. set-executionpolicy remotesigned

二、安装Oh-my-posh

安装Oh-my-posh,下面命令挨个执行:

  1. choco install ConEmu
  2. Install-Module oh-my-posh -Scope CurrentUser
  3. Install-Module posh-git -Scope CurrentUser

设置Profile脚本:

  1. if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
  2. notepad $PROFILE

使用记事本打开生成的文件,加入下面代码:

  1. Import-Module posh-git
  2. Import-Module oh-my-posh
  3. Set-Theme pure

可以看到终端已经变为:
image.png

三、设置主题

Oh-my-posh支持以下主题:

  1. Name Type Location
  2. ---- ---- --------
  3. Agnoster Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Agnoster.psm1
  4. Avit Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Avit.psm1
  5. Darkblood Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Darkblood.psm1
  6. Fish Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Fish.psm1
  7. Honukai Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Honukai.psm1
  8. Paradox Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Paradox.psm1
  9. Powerlevel10k-Classic Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Powerlevel10k-Classic.psm1
  10. Powerlevel10k-Lean Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Powerlevel10k-Lean.psm1
  11. PowerLine Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\PowerLine.psm1
  12. pure Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\pure.psm1
  13. robbyrussell Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\robbyrussell.psm1
  14. Sorin Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\Sorin.psm1
  15. tehrob Defaults C:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.399\Themes\tehrob.psm1

使用以下命令可以快速切换终端样式(不保存):

  1. Set-Theme <ThemeName>

设置主题:
image.png
带Git的样式:
image.png

四、设置字体

可以安装一些比较漂亮的字体,比如 Nerd.Font、Fira Code

  1. choco install fira-code

安装字体后,Windows Terminal中配置即可:

  1. {
  2. "defaults":
  3. {
  4. "fontFace" : "'Fira Code', 'Sarasa Term SC Regular', Consolas, 'Courier New', monospace",
  5. "fontSize" : 16,
  6. }
  7. }

五、主题相关配置

使用Show-ThemeColors命令显示正在使用的主题的配色:
image.png
使用 $ThemeSettings 显示当前主题的配置

  1. C:\Windows\System32> $ThemeSettings
  2. PromptSymbols : {UNCSymbol, HomeSymbol, ElevatedSymbol, SegmentSeparatorForwardSymbol...}
  3. Options : {ConsoleTitle, PreserveLastExitCode, OriginSymbols}
  4. CurrentUser : quanzaiyu
  5. CurrentThemeLocation : D:\Users\quanzaiyu\Documents\WindowsPowerShell\Modules\oh-my-posh\2.0.478\Themes\Agnoster.psm1
  6. ErrorCount : 0
  7. Colors : {AdminIconForegroundColor, PromptBackgroundColor, PromptHighlightColor, GitLocalChangesColor...}
  8. CurrentHostname : DESKTOP-KR4DIJ4
  9. GitSymbols : {OriginSymbols, LocalWorkingStatusSymbol, LocalDefaultStatusSymbol, BranchUntrackedSymbol...}
  10. MyThemesLocation : D:\Users\quanzaiyu\Documents\WindowsPowerShell\PoshThemes

显示Git样式配置:

  1. $GitPromptSettings

参考资料