前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >打造一个华丽又实用的 PowerShell / Bash 命令输入环境

打造一个华丽又实用的 PowerShell / Bash 命令输入环境

作者头像
科控物联
发布2024-04-18 19:04:37
1570
发布2024-04-18 19:04:37
举报
文章被收录于专栏:科控自动化科控自动化

重装系统,再折腾一下。

安裝 Windows Terminal 最新版

代码语言:javascript
复制
winget install Microsoft.WindowsTerminal --accept-source-agreements

或者store安装也行

安裝 PowerShell 7 最新版

代码语言:javascript
复制
winget install Microsoft.PowerShell

安裝 Oh My Posh 工具

代码语言:javascript
复制
winget install JanDeDobbeleer.OhMyPosh

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

重開 Windows PowerShell 並安裝 Oh My Posh 所需的字型

執行以下命令會直接安裝 CascadiaCode 字型!

代码语言:javascript
复制
oh-my-posh.exe font install CascadiaCode

工具安裝完畢後,請改用 Windows Terminal 開啟,你可以選擇 Windows PowerShell (代表 5.1 版) 或是 PowerShell (代表 7.x 版)!

調整 Windows Terminal 設定

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

建議修改 PowerShell 的啟動參數,命令列的部分加入 -NoLogo 參數:

代码语言:javascript
复制
"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo

安裝 Terminal-Icons 模組

代码语言:javascript
复制
Install-Module -Name Terminal-Icons -Repository PSGallery -Force

這個模組可以讓你在執行 Get-ChildItem (dir) 的時候會在檔案資料夾上顯示檔案類型圖示。

安裝 PSReadLine 模組

優化 PowerShell 環境的利器,不過只能在 PowerShell 7 執行

代码语言:javascript
复制
Install-Module PSReadLine -AllowPrerelease -Force

設定好用的 PowerShell 命令輸入環境

初始化 $PROFILE 啟動設定檔

代码语言:javascript
复制
[System.IO.Directory]::CreateDirectory([System.IO.Path]::GetDirectoryName($PROFILE))if (-not (Test-Path -Path $PROFILE -PathType Leaf)) {  New-Item $PROFILE -Force}

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

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

代码语言:javascript
复制
(Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/doggy8088/d3f3925452e2d7b923d01142f755d2ae/raw/aabe600ed2adccb43165228b8c8ced6e88ac9fc0/$PROFILE_PSReadLine.ps1').Content | Out-File -LiteralPath $PROFILE -Append -Encoding utf8

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

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

代码语言:javascript
复制
(Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/doggy8088/2bf2a46f7e65ae4197b6092df3835f21/raw/e5e73da6aabaf51ae49c641f5ca409f38f660443/$PROFILE_ArgumentCompleter.ps1').Content | Out-File -LiteralPath $PROFILE -Append -Encoding utf8

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

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

代码语言:javascript
复制
(Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/doggy8088/553c4548492b63e4ccbe30d843de85f6/raw/5b8492883519ffbd74557e26d7eaf73dc2692c23/$PROFILE_Cmdlets_Functions.ps1').Content | Out-File -LiteralPath $PROFILE -Append -Encoding utf8

匯入 Terminal-Icons 模組

代码语言:javascript
复制
'Import-Module -Name Terminal-Icons' | Out-File -LiteralPath $PROFILE -Append -Encoding utf8

啟動 Oh My Posh 的 Shell 環境

立即啟用 Oh My Posh 的命令列提示 (Prompt)

代码语言:javascript
复制
oh-my-posh init pwsh | Invoke-Expression

啟動後,你就可以透過 Get-PoshThemes 快速顯示所有的 Themes 樣式,選一個自己喜歡的版本!😍

假設你選擇 atomicBit 的話,就可以這樣重新啟動:

代码语言:javascript
复制
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\atomicBit.omp.json"  | Invoke-Expression

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

代码语言:javascript
复制
'oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\atomicBit.omp.json"  | Invoke-Expression' | Out-File -LiteralPath $PROFILE -Append -Encoding utf8

重新啟動 Windows Terminal 或重新載入 $PROFILE 啟動設定檔

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

代码语言:javascript
复制
. $PROFILE
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-04-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 科控物联 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 設定好用的 PowerShell 命令輸入環境
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档