Mac
上使用 iTerm2 + oh-my-zsh
,也希望 Windows
上有好用的 Terminal
,虽然知道有 Windows Terminal
,但懒的折腾也就没整。不过最近电脑频繁死机,不得以重装后,顺便把 Windows Terminal
搞了,不过遇到各种乱七八糟的问题,在这里跟大分享一下。
从Microsoft App Store 安装Windows Terminal
。
在 NERD FONTS 中选择任意你喜欢的字体并进行安装,注意: 需要与后面相关的字体设置相匹配
安装方法:
- 下载相应的字体并解压缩
- 选中所有的字体
- 右击选择 “安装”
“defaultProfile”: 将
Power Shell 7
设置为默认终端
“profiles” :设置外观、字体、主题等
“useAcrylic”:用于设置毛玻璃特效
“colorScheme”:设置主题
“face”:设置字体
"defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"profiles":
{
"defaults":
{
"acrylicOpacity": 0.5,
"closeOnExit": "graceful",
"colorScheme": "Homebrew",
"cursorColor": "#FFFFFF",
"cursorShape": "bar",
"font":
{
"face": "JetBrainsMono NF",
"size": 11
},
"hidden": false,
"historySize": 9001,
"padding": "5, 5, 20, 25",
"snapOnInput": true,
"startingDirectory": ".",
"useAcrylic": false
},
Windows Terminal
主题本文主题为经他人修改的 Homebrew 主题
将该配置文件放置于"schemes":
项目下
{
"background": "#283033",
"black": "#000000",
"blue": "#6666E9",
"brightBlack": "#666666",
"brightBlue": "#0000FF",
"brightCyan": "#00E5E5",
"brightGreen": "#00D900",
"brightPurple": "#E500E5",
"brightRed": "#E50000",
"brightWhite": "#E5E5E5",
"brightYellow": "#E5E500",
"cursorColor": "#FFFFFF",
"cyan": "#00A6B2",
"foreground": "#00FF00",
"green": "#00A600",
"name": "Homebrew",
"purple": "#B200B2",
"red": "#FC5275",
"selectionBackground": "#FFFFFF",
"white": "#BFBFBF",
"yellow": "#999900"
},
在 Power Shell 的 GitHub
项目中选择适合自己的 Power Shell
进行安装。
Powe Shell
插件打开
Windows Terminal
,进入Power Shell
# 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh
Install-Module -Name PSReadLine -Scope CurrentUser
# 2. 安装 posh-git 包,让你的 git 更好用
Install-Module posh-git -Scope CurrentUser
# 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅
Install-Module oh-my-posh -Scope CurrentUser
Power Shell
的配置文件Set-PoshPrompt :用于设置
oh-my-posh
主题
code $Profile
<#
* FileName: Microsoft.PowerShell_profile.ps1
* Author: 刘 鹏
* Email: [email protected]
* Date: 2021, Aug. 21
* Copyright: No copyright. You can use this code for anything with no warranty.
#>
#------------------------------- Import Modules BEGIN -------------------------------
# 引入 posh-git
Import-Module posh-git
# 引入 oh-my-posh
Import-Module oh-my-posh
# 引入 ps-read-line
Import-Module PSReadLine
# 设置 PowerShell 主题
# Set-PoshPrompt ys
Set-PoshPrompt paradox
#------------------------------- 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 -------------------------------
#------------------------------- Functions BEGIN -------------------------------
# Python 直接执行
$env:PATHEXT += ";.py"
# 更新系统组件
function Update-Packages {
# update pip
Write-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan
$a = pip list --outdated
$num_package = $a.Length - 2
for ($i = 0; $i -lt $num_package; $i++) {
$tmp = ($a[2 + $i].Split(" "))[0]
pip install -U $tmp
}
# update TeX Live
$CurrentYear = Get-Date -Format yyyy
Write-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyan
tlmgr update --self
tlmgr update --all
# update Chocolotey
Write-Host "Step 3: 更新 Chocolatey" -ForegroundColor Magenta -BackgroundColor Cyan
choco outdated
}
#------------------------------- Functions END -------------------------------
#------------------------------- Set Alias BEGIN -------------------------------
# 1. 编译函数 make
function MakeThings {
nmake.exe $args -nologo
}
Set-Alias -Name make -Value MakeThings
# 2. 更新系统 os-update
Set-Alias -Name os-update -Value Update-Packages
# 3. 查看目录 ls & ll
function ListDirectory {
(Get-ChildItem).Name
Write-Host("")
}
Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem
# 4. 打开当前工作目录
function OpenCurrentFolder {
param
(
# 输入要打开的路径
# 用法示例:open C:\
# 默认路径:当前工作文件夹
$Path = '.'
)
Invoke-Item $Path
}
Set-Alias -Name open -Value OpenCurrentFolder
#------------------------------- Set Alias END -------------------------------
#------------------------------- Set Network BEGIN -------------------------------
# 1. 获取所有 Network Interface
function Get-AllNic {
Get-NetAdapter | Sort-Object -Property MacAddress
}
Set-Alias -Name getnic -Value Get-AllNic
# 2. 获取 IPv4 关键路由
function Get-IPv4Routes {
Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'}
}
Set-Alias -Name getip -Value Get-IPv4Routes
# 3. 获取 IPv6 关键路由
function Get-IPv6Routes {
Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'}
}
Set-Alias -Name getip6 -Value Get-IPv6Routes
#------------------------------- Set Network END -------------------------------
oh-my-posh
主题选1个自己喜欢的主题,并对
Set-PoshPrompt
进行修改
Get-PoshThemes
JetBrainsMono NF
。oh-my-posh
的 bug
,因为不启用 oh-my-posh
时,会默认显示 python
的虚拟环境。oh-my-posh
主题后,python
虚拟环境不再显示。oh-my-posh
相应主题的配置文件oh-my-posh
对应主题的 python 模块,二是在对应主题添加 Environment Variable 模块xxxxxxx\Documents\PowerShell\Modules\oh-my-posh\5.7.0\themes
{
"type": "python",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#100e23",
"background": "#906cff",
"properties": {
"prefix": " \uE235 ",
"display_virtual_env": true,
"display_default": true,
"display_mode": "context"
}
},
找到
anaconda
的环境变量
dir env:
在
properties
配置相应的环境变量
{
"type": "envvar",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#193549",
"background": "#FF6E6E",
"properties": {
"prefix": "\uE235",
"var_name": "CONDA_DEFAULT_ENV"
}
},
禁用
"display_host"
即可
{
"type": "session",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#100e23",
"background": "#ffffff",
"properties": {
"display_user": true,
"display_host": false
}
},
[1] Oh my Posh 3——易于自定义主题的Powershell美化工具
[2] Windows Terminal 完美配置 PowerShell 7.1
[3] PowerShell 界面美化
[4] Windows Terminal + oh-my-posh模块美化官方教程集锦以及常见问题(问题收集中)
[5] 简单配置与美化Powershell和Terminal
[6] Powershell 主题美化学习过程