赞
踩
安装terminal
微软商店下载安装terminal
进入terminal,打开设置。
- {
- "$schema": "https://aka.ms/terminal-profiles-schema",
- // global settings
- "profiles": {
- // profile settings
- "defaults": {
- // default settings
- },
- "list": [
- // profile lists
- ]
- },
- "schemes": [], // color scheme settings
- "keybindings": [
- // key bindings
- ]
- }

$schema
是软件生成的固定内容。//global settings
位置可以设置很多终端表现相关的设置选项。profile
处设置 Shell 相关的配置选项。schemes
处设置颜色配置相关的配置选项。keybindings
设置键盘组合键相关的配置选项。分享一下我的配置文件(淳朴简约,没有花里胡哨的界面配置,节约性能哈哈):
- {
- "$help": "https://aka.ms/terminal-documentation",
- "$schema": "https://aka.ms/terminal-profiles-schema",
- "actions":
- [
- {
- "command":
- {
- "action": "copy",
- "singleLine": false
- },
- "keys": "ctrl+c"
- },
- {
- "command": "paste",
- "keys": "ctrl+v"
- },
- {
- "command": "find",
- "keys": "ctrl+shift+f"
- },
- {
- "command":
- {
- "action": "splitPane",
- "split": "auto",
- "splitMode": "duplicate"
- },
- "keys": "alt+shift+d"
- }
- ],
- "copyFormatting": "none",
- "copyOnSelect": true,
- "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
- "focusFollowMouse": true,
- "newTabMenu":
- [
- {
- "type": "remainingProfiles"
- }
- ],
- "profiles":
- {
- "defaults":
- {
- "adjustIndistinguishableColors": "indexed",
- "backgroundImage": "desktopWallpaper",
- "backgroundImageOpacity": 0.9,
- "font":
- {
- "face": "Cascadia Code",
- "weight": "medium"
- }
- },
- "list":
- [
- {
- "commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
- "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
- "hidden": false,
- "name": "Windows PowerShell"
- },
- {
- "commandline": "%SystemRoot%\\System32\\cmd.exe",
- "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
- "hidden": false,
- "name": "\u547d\u4ee4\u63d0\u793a\u7b26"
- },
- {
- "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
- "hidden": false,
- "name": "Azure Cloud Shell",
- "source": "Windows.Terminal.Azure"
- },
- {
- "guid": "{4dd1e689-b517-5f39-947d-78e8a8bdf958}",
- "hidden": false,
- "name": "Ubuntu 20.04.6 LTS",
- "source": "CanonicalGroupLimited.Ubuntu20.04LTS_79rhkp1fndgsc"
- },
- {
- "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
- "hidden": true,
- "name": "Ubuntu-20.04",
- "source": "Windows.Terminal.Wsl"
- },
- {
- "guid": "{a5a97cb8-8961-5535-816d-772efe0c6a3f}",
- "hidden": false,
- "name": "Arch",
- "source": "Windows.Terminal.Wsl"
- }
- ]
- }
- }

更多配置参考官方文档(Windows 终端常规配置文件设置 | Microsoft Learn)
oh-my-posh
2.参考文档
Microsoft官方教程 :使用 Oh My Posh 为 PowerShell 或 WSL 设置自定义提示符
oh-my-posh官方教程:oh-my-posh
首先去 Github
下载 nerd-fonts 字体。字体设置的目的是为了保证在安装了 oh-my-posh
后不产生特殊字符显示的问题。
点击 Release
,找到 FiraCode.zip
进行下载。解压,安装字体
WindowsTerminal
默认字体oh-my-posh
PSReadLine 、oh-my-posh 和 posh-git 这三个模块来为 PowerShell 带来类似于 Mac 上的 oh my zsh 的体验,包括语法高亮、错误提示、多行编辑、快捷键绑定、自动补全、历史搜索、Git 扩展显示等功能。
Get-InstalledModule
PSReadLine 提供了语法高亮、错误提示、多行编辑、键绑定、历史记录搜索等功能:
- #用管理员打开powershell
- Install-Module PSReadLine
posh-git 可以在 PowerShell 中显示 Git 状态信息,并提供 Git 命令的自动补全:
Install-Module posh-git
oh-my-posh
PowerShell命令行美化
oh-my-posh
是 PowerShell 主题管理工具,posh-git
可以实现类似 oh-my-zsh
一样的 Git
命令增强工具(命令别名和显示分支信息等),但是,oh-my-posh
基于 posh-git
的,所以两个都要安装。
提示:请提前以管理员身份运行 PowerShell
1.安装oh-my-posh
winget install JanDeDobbeleer.OhMyPosh -s winget
2.查看主题列表,并试用主题
Get-PoshThemes
很好,报错了,查看文档,文档
对于要重新加载的 PATH
终端,建议重新启动终端。如果 oh-my-posh 未被识别为命令,您可以再次运行安装程序,或手动将其添加到您的 PATH
.例如:
$env:Path += "C:\Users\user\AppData\Local\Programs\oh-my-posh\bin"
不用代码也可以在高级系统设置里添加环境变量:
添加好,终端要都关掉再启动,不然环境变量更新不上去。
oh-my-posh的安装路径通常默认在C:\Program Files (x86)\oh-my-posh\(或者C:\Users\user\AppData\Local\Programs\oh-my-posh)
在上面添加了环境变量了,如果还是无法识别Get-PoshThemes命令,就直接卸载重装oh-my-posh。
- #powershell卸载
- winget uninstall JanDeDobbeleer.OhMyPosh
- #powershell安装
- winget install JanDeDobbeleer.OhMyPosh
手动安装也可以,这里提供一下github下ohmyposh下载地址,一般不管用命令行还是手动安装会自动设置环境变量,如果没有按上面自行设置。
这个小插曲搞定,继续下一步:
创建配置文件
New-Item -Path $PROFILE -Type File -Force
编辑配置文件
notepad $PROFILE
这时应该会打开记事本,将步骤二里的的代码添加进去并保存,例如保存 wholespace 主题,将如下代码加入记事本:
- [System.Console]::OutputEncoding=[System.Text.Encoding]::GetEncoding(65001) # 配置默认编码
- oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\atomic.omp.json" | Invoke-Expression # 配置oh-my-posh的theme
你可以修改atomic.omp.json
为 oh-my-posh
的主题文件,你可以在 Themes | Oh My Posh 这里找到自己喜欢的主题名称。配置完成后关闭 terminal
重新开发就可以看到新的主题了。
关于配置文件我们也可以手动打开,配置文件的路径:C:\Users\user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
如果你用的是 PowerShell
那么配置文件 C:\Users\user\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
重新打开终端报错一下:
Get-ExecutionPolicy
Bypass
:Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
这里简单介绍一下powershell的执行策略:
这些策略仅在 Windows 平台上执行。 PowerShell 执行策略如下:
AllSigned
- 脚本可以运行。
- 要求所有脚本和配置文件都由受信任的发布者签名,包括在本地计算机上编写的脚本。
- 在运行来自尚未分类为可信或不可信的发布者的脚本之前会提示你。
- 存在运行已签名的恶意脚本的风险。
Bypass
- 不阻止任何操作,并且没有任何警告或提示。
- 此执行策略专为将 PowerShell 脚本内置到较大应用程序中的配置,或以 PowerShell 为具有自己的安全模型的程序的基础的配置而设计。
Default
- 设置默认执行策略。
- Restricted(适用于 Windows 客户端)。
- RemoteSigned(适用于 Windows 服务器)。
RemoteSigned
- Windows 服务器计算机的默认执行策略。
- 脚本可以运行。
- 需要受信任的发布者对从 Internet 下载的脚本和配置文件(包括电子邮件和即时消息程序)的数字签名。
- 在本地计算机上编写且不是从 Internet 下载的脚本不需要数字签名。
- 如果脚本已解除阻止(例如通过使用
Unblock-File
cmdlet),则运行从 Internet 下载且未签名的脚本。- 存在运行来自 Internet 以外来源的未签名脚本以及可能存在恶意的签名脚本的风险。
Restricted
- Windows 客户端计算机的默认执行策略。
- 允许单个命令,但不允许脚本。
- 阻止运行所有脚本文件,包括格式和配置文件 (
.ps1xml
)、模块脚本文件 (.psm1
) 和 PowerShell 配置文件 (.ps1
)。
Undefined
- 当前作用域内没有设置执行策略。
- 如果所有作用域内的执行策略均为 Undefined,则对于 Windows 客户端,有效执行策略为 Restricted;对于 Windows Server,有效执行策略为 RemoteSigned。
Unrestricted
- 非 Windows 计算机的默认执行策略,无法更改。
- 未签名的脚本可以运行。 存在运行恶意脚本的风险。
- 在运行非来自本地 Intranet 区域的脚本和配置文件之前警告用户。
重新打开成功!
powershell配置文件地址是 C:\Users\<userName>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
。
作用是在 PowerShell 启动时运行一些自定义的设置,比如导入模块、设置别名、定义函数等。
粘贴以下配置内容,可以参考注释根据自己需求修改或者删除:
- <#
- * FileName: Microsoft.PowerShell_profile.ps1
- * Author: Devil
- * Email: 1093008057@qq.com
- * Date: 2024.04.07
- * Copyright: You can use this code for anything with no warranty.
- #>
-
- #------------------------------- Import Modules BEGIN -------------------------------
- # 引入 ps-read-line
- Import-Module PSReadLine
-
- # 引入 posh-git
- Import-Module posh-git
-
- # 引入 oh-my-posh
- Import-Module oh-my-posh
-
- # 设置 PowerShell 主题
- # Set-PoshPrompt ys
- Set-PoshPrompt emodipt-extend
- #------------------------------- Import Modules END -------------------------------
-
- #------------------------------- Set Hot-keys BEGIN -------------------------------
- # 设置预测文本来源为历史记录
- Set-PSReadLineOption -PredictionSource History
-
- # 每次回溯输入历史,光标定位于输入内容末尾
- Set-PSReadLineOption -HistorySearchCursorMovesToEnd
-
- # 设置 Tab 为菜单补全和 Intellisense
- Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
-
- # 设置 Ctrl+d 为退出 PowerShell
- Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit
-
- # 设置 Ctrl+z 为撤销
- Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
-
- # 设置向上键为后向搜索历史记录
- Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
-
- # 设置向下键为前向搜索历史纪录
- Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
- #------------------------------- Set Hot-keys END -------------------------------

上述就是关于Windows Terminal 美化 PowerShell 内容了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。