Theme NexT works best with JavaScript enabled

ShunNien's Blog

不積跬步,無以致千里;不積小流,無以成江海。

0%

PowerShell 美化配置

PowerShell 美化配置

powershell 是 windows 目前很好用的工具,可以調整一些設定與安裝一些模組功能來美化它。

Terminal 與 Shell

  • Windows 的命令提示字元 (Command Prompt),實際上就是打開了一個終端 (Terminal)
  • 在終端裡面輸入命令,得到結果的互動程式,就是命令行解釋器 (Shell)

主題樣式

個人推薦的主題樣式是 oh-my-posh,此主題類似 Mac OS X 的 oh-my-zsh,不過因為 oh-my-posh 有部分的指令倚賴 post-git,所以需要兩者都安裝。注意需要使用系統管理員權限啟動 powershell 來安裝,安裝指令如下:

1
2
3
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser
Set-Theme Paradox

安裝好之後,不滿意 Paradox 樣式的話,可以自行更換,oh-my-posh 提供了 10 個主題樣式,只要在 PowerShell 中輸入 Theme 就可以查看有哪些主題樣式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
❯ theme

Name Type Location
---- ---- --------
Agnoster Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Agnoster.psm1
Avit Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Avit.psm1
Darkblood Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Darkblood.psm1
Fish Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Fish.psm1
Honukai Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Honukai.psm1
Paradox Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Paradox.psm1
PowerLine Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\PowerLine.psm1
pure Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\pure.psm1
robbyrussell Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\robbyrussell.psm1
Sorin Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\Sorin.psm1
tehrob Defaults C:\Program Files\WindowsPowerShell\Modules\oh-my-posh\2.0.263\Themes\tehrob.psm1

想要更換主題樣式的話,設定指令如下,Set-Theme 指令後接上想要設定主題樣式名稱

1
Set-Theme Darkblood

設定每次啟用 Powershell 的主題

上述設定完成後,現在使用的 PowerShell 畫面變漂亮了,但是 PowerShell 每次執行都是原始介面,所以需要設定,以下是設定步驟

新增或修改 $profile 檔案

先開啟 powershell 輸入以下指令

1
2
3
4
# 如果之前沒有檔案,就建立一個新檔案
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
# 用 vscode 開啟檔案
code $profile

上述指令會建立 profile 檔案,但是需要注意的是,使用管理者權限執行的路徑與現在使用者的 profile 路徑不同,在 powershell 內,$profile 預設就是目前使用者的路徑(等同 $profile.CurrentUserCurrentHost ),一共有四種選擇,但是檔案路徑組合(32位元與64位元的搭配)會有多達 9 種,有興趣的可以參考此篇文章

  • $profile.CurrentUserAllHosts
  • $profile.CurrentUserCurrentHost
  • $profile.AllUsersAllHosts
  • $profile.AllUsersCurrentHost
1
2
3
4
5
6
❯ $PROFILE | Select-Object -Property *
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\Shunnien Yu\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\Shunnien Yu\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length : 81

想要設定為不管是否使用管理者權限執行都是相同的畫面的話,需要設定 AllUsersAllHosts ,設定這個檔案後,其他檔案就可以不用設定了,網路上有可以參考的設定範本,以下是我自己的設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
chcp 65001
Set-PSReadlineOption -EditMode Emacs
function which($name) { Get-Command $name | Select-Object Definition }
function rmrf($item) { Remove-Item $item -Recurse -Force }
function mkfile($file) { "" | Out-File $file -Encoding ASCII }
function getMyDoc() { Set-Location "C:\Users\Shunnien Yu\Documents\" }
function getGitOneLineGraphLog() { git log --oneline --all --graph --decorate }
Import-Module posh-git
Import-Module oh-my-posh
Import-Module Get-ChildItemColor
Set-Alias l Get-ChildItemColor -option AllScope
Set-Alias ls Get-ChildItemColorFormatWide -option AllScope
Set-Alias ... getMyDoc -option AllScope
Set-Alias ggl getGitOneLineGraphLog -option AllScope
Set-Theme PowerLine

字型

設定為上述的檔案後,powershell 在有 git 版控的資料夾路徑時,會使用 PowerLine 字體,但是由於目前的字型不支援會像下圖

no powerline font

所以需要裝有支援 PowerLine 的字型,我個人使用 Sarasa Gothic (更纱黑体 / 更紗黑體 / 更紗ゴシック) 字型,可以由Sarasa Gothic 字型的 Github Release 下載

Sarasa Gothic github release

之後直接在 Powershell 的 Properties 內設定字型,我個人使用 Sarasa Term TC

powershell properties font set

設定好之後,powershell 的畫面中就可以顯示符號

powershll have powerline font

PowerShell 配色

都設定好之後,如果對於 powershell 的配色不滿意的話,可以安裝 ColorTool 這個微軟提供的工具,安裝方式直接下載 Release,解壓縮後的資料夾路徑加入倒環境變數中,直接就可以執行指令 colortool
想添加新的樣式的話,可以上 iTerm2-Color-Schemes 尋找符合各自需求的樣式,加入至 schemes 資料夾內即可;假如是透過 chocolatey 安裝的話,添加樣式的路徑在 C:\ProgramData\chocolatey\lib\colortool\content\schemes

參考資料

歡迎關注我的其它發布渠道