18

如何打造一個華麗又實用的 PowerShell 命令輸入環境

 2 years ago
source link: https://blog.miniasp.com/post/2021/11/24/PowerShell-prompt-with-Oh-My-Posh-and-Windows-Terminal
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

每次看到 Scott Hanselman 那套華麗的 PowerShell 命令輸入環境,就覺得自己一定也要設定一個類似的環境,不但使用起來讓人覺得神清氣爽,在同事朋友面前也會顯得出類拔萃。今天這篇文章,我就把設定的過程做個簡單的筆記,照著設定也可以做到一樣的效果。

安裝必備工具

  1. 安裝 Windows Terminal 最新版

    winget install Microsoft.WindowsTerminal
    
  2. 安裝 PowerShell Core 最新版

    winget install Microsoft.PowerShell
    
  3. 安裝 Oh My Posh 工具

    winget install JanDeDobbeleer.OhMyPosh
    

    自動安裝 oh-my-posh.exe 主程式,並設定 PATH 環境變數。

  4. 安裝 oh-my-posh 模組

    Install-Module oh-my-posh -Scope CurrentUser -Force
    Import-Module oh-my-posh
    

    第一次執行 Import-Module oh-my-posh 會自動下載 oh-my-posh.exe 執行檔,之後每次執行也會自動更新。

    如果 oh-my-posh 模組太舊,可以用以下命令更新:

    Update-Module oh-my-posh
    
  5. 安裝 Terminal-Icons 模組

    Install-Module -Name Terminal-Icons -Repository PSGallery -Force
    

    執行 Get-ChildItem 的時候會在檔案與資料夾上顯示檔案類型圖示。

  6. 安裝 PSReadLine 模組

    Install-Module PSReadLine -AllowPrerelease -Force
    

    優化 PowerShell 環境的利器。

  7. 安裝 Nerd Fonts 字型檔

    這個字型包含大量的 glyphs (icons),可以讓你的命令列環境顯示精美的圖示!👍

    Invoke-WebRequest -Uri "https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/CascadiaCode.zip" -OutFile CascadiaCode.zip
    $tmpFolder = New-TemporaryFile | %{ rm $_; mkdir $_ }
    Expand-Archive -LiteralPath CascadiaCode.zip -DestinationPath $tmpFolder
    Remove-Item -Path .\CascadiaCode.zip
    
    # https://www.powershellgallery.com/packages/PSWinGlue
    Install-Module -Name PSWinGlue -Force
    Install-Font -Scope User -Path $tmpFolder
    
  8. 調整 Windows Terminal 設定

    請將 Windows PowerShellPowerShell CoreWSL外觀 (Appearance) 調整使用 CaskaydiaCove Nerd Font Mono 字型 (Font face)!

上述步驟都設定完成後,請關閉 Windows Terminal 之後重開!

設定好用的 PowerShell 命令與函式

  1. 啟用 PSReadLine 並啟用超強自動完成功能

    你可以直接將我的 $PROFILE_PSReadLine.ps1 內容加入到 $PROFILE 啟動設定檔中!

  2. 設定各種命令自動完成提示

    你可以直接將我的 $PROFILE_ArgumentCompleter.ps1 內容加入到 $PROFILE 啟動設定檔中!

  3. 加上幾個我自訂的 Cmdlets / Functions 與停用兩個不太實用的 Cmdlet Aliases

    你可以直接將我的 $PROFILE_Cmdlets_Functions.ps1 內容加入到 $PROFILE 啟動設定檔中!

  4. 讓 PowerShell 終端機視窗開啟時感覺比較乾淨

    首先,建議你在 Windows Terminals 設定的地方修改 PowerShell Core 的啟動參數,命令列的部分加入 -NoLogo 參數:

    C:\Program Files\PowerShell\7\pwsh.exe -NoLogo
    

    建議也在 $PROFILE 啟動設定檔中加入 Clear-Host 命令 (Cmdlet),這樣可以讓 PowerShell 終端機視窗開啟時感覺比較乾淨。

    Clear-Host
    

你可以使用點表示法重新載入 $PROFILE 啟動設定檔:

. $PROFILE

設定 PowerShell 命令列提示

  1. 匯入 Terminal-Icons 模組

    請將以下命令加入到 $PROFILE 啟動設定檔中:

    Import-Module -Name Terminal-Icons
    
  2. 啟動 Oh My Posh 的 Shell 環境

    你可以下載我的 Oh My Posh 設定檔 (ohmyposhv3-will.omp.json)

    Invoke-WebRequest -Uri "https://gist.githubusercontent.com/doggy8088/17e87b9be99639ffb52bbe0709f46b93/raw/a1631aeac5638424a20bef54c9f0abc045b9e611/ohmyposhv3-will.omp.json" -OutFile "~/.ohmyposhv3-will.omp.json"
    

    測試套用 Oh My Posh 的命令列提示 (Prompt)

    Set-PoshPrompt -Theme ~/.ohmyposhv3-will.omp.json
    

    然後將以下命令加入到 $PROFILE 啟動設定檔中

    Import-Module oh-my-posh
    Set-PoshPrompt -Theme ~/.ohmyposhv3-will.omp.json
    

Set-PoshPrompt 無法使用時可改用 oh-my-posh.exe 來啟動

  1. 測試預設的 Oh My Posh 提示

    oh-my-posh --init --shell pwsh | Invoke-Expression
    
  2. 下載我的 Oh My Posh 設定檔 (ohmyposhv3-will.omp.json)

    Invoke-WebRequest -Uri "https://gist.githubusercontent.com/doggy8088/17e87b9be99639ffb52bbe0709f46b93/raw/a1631aeac5638424a20bef54c9f0abc045b9e611/ohmyposhv3-will.omp.json" -OutFile "~/.ohmyposhv3-will.omp.json"
    

    你也可以到 Oh My Posh 專案的下載現成的設定檔

  3. 套用新的命令列提示 (Prompt)

    oh-my-posh --init --shell pwsh --config ~/.ohmyposhv3-will.omp.json | Invoke-Expression
    
  4. 然後將上述命令加入到 $PROFILE 啟動設定檔中

    notepad $PROFILE
    
  5. 使用點表示法重新載入 $PROFILE 啟動設定檔

    . $PROFILE
    

設定 WSL 下的 Bash 也使用 Oh My Posh 提示

  1. 安裝 Oh My Posh 到 Linux

    sudo wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-amd64 -O /usr/local/bin/oh-my-posh
    sudo chmod +x /usr/local/bin/oh-my-posh
    
  2. 下載我的 Oh My Posh 設定檔 (ohmyposhv3-will.omp.json)

    先下載官方的版本:

    mkdir ~/.poshthemes
    wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip -O ~/.poshthemes/themes.zip
    unzip ~/.poshthemes/themes.zip -d ~/.poshthemes
    chmod u+rw ~/.poshthemes/*.json
    rm ~/.poshthemes/themes.zip
    

    再下載我的版本:

    curl -s 'https://gist.githubusercontent.com/doggy8088/17e87b9be99639ffb52bbe0709f46b93/raw/a1631aeac5638424a20bef54c9f0abc045b9e611/ohmyposhv3-will.omp.json' -o ~/.poshthemes/ohmyposhv3-will.omp.json
    
  3. 套用新的命令列提示 (Prompt)

    eval "$(oh-my-posh --init --shell bash --config ~/.poshthemes/ohmyposhv3-will.omp.json)"
    
  4. 然後將上述命令加入到 ~/.bashrc 啟動設定檔中


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK