效果展示

每次在使用 hexo 时,总需要 CD 到博客目录下或者前往博客目录打开 PowerShell。
每次写博客都要这样操作,显得十分繁琐。
同时,每次看到丑陋的 PowerShell 界面,总提不起写作的兴趣。
这可不是拖更博客的理由哈哈哈,说白了还是懒。
为了解决这一痛点,我研究了 Windows Terminal,
利用 Windows Terminal 的设置美化了界面,
实现了直接打开 PowerShell 以 hexo 目录进行操作。
废话不多说,让我来教大家如何安装和设置吧。
步骤 1:下载 & 安装 Windows Terminal

资源下载:
{% btns rounded grid5 %}
{% cell 本站下载, https://dogecloud.yuqi.work/app%2FMicrosoft.WindowsTerminal_2021.1214.340.0_neutral_~_8wekyb3d8bbwe.Msixbundle, fas fa-download %}
{% cell 蓝奏云下载, https://wwi.lanzouw.com/iEViuy4buih, fas fa-download %}
{% endbtns %}
{% tip warning %}必须运行 Windows 1903 (build>= 10.0.18362.0) 或更高版本才能运行 Windows 终端{% endtip %}
首先需要解决的是你的系统环境问题。如果你有经常升级系统的话,这个问题就不会给你造成影响。
如果你的 Windows 版本不够的话,可以使用 windows 易升进行升级,这里不进行赘述。
打开你的 windows 应用商店(Microsoft Store)搜索 Windows Terminal 进行下载 & 安装。
安装完成后,在C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_版本号_x64__xxxxxxxx 目录下,打开WindowsTerminal.exe 即可打开软件。
右键创建快捷方式到桌面,或固定到任务栏中。
步骤 2:下载 & 安装 PowerShell Core
为了获取更佳的 PowerShell 体验,需下载 PowerShell core 版本。
{% btns rounded grid5 %}
{% cell 最新版下载, https://github.com/PowerShell/PowerShell/tags, fas fa-download %}
{% cell 蓝奏云下载, https://wwi.lanzouw.com/iO08ey4bx7e, fas fa-download %}
{% endbtns %}
安装完成后,在C:\Program Files\PowerShell\7-preview目录下,打开pwsh.exe即可打开软件。
步骤 3:下载 & 安装Fira Code Nerd Font字体
这里仅推荐一款字体:Fira Code Nerd Font。该字体支持 ligature 连字功能,而且是一款专门为代码显示准备的字体,该字体也支持很多有趣的特殊字符,非常适合在终端里使用。该字体开源,广受海内外程序员好评!
{% btns rounded grid5 %}
{% cell Github下载, https://link.zhihu.com/?target=https%3A//github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/FiraCode.zip, fas fa-download %}
{% cell 蓝奏云下载, https://wwi.lanzouw.com/iWVg2y4cblc, fas fa-download %}
{% endbtns %}
步骤 4:配置 PowerShell Core
安装 Powershell 插件
打开刚装好的新版 powershell,逐行输入命令。
# 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
安装过程可能有点慢,好像卡住了一样,但是请耐心等待几分钟。等不及的同学自行搜索科学方法访问 GitHub.
安装时系统会提问是否继续,不用管它直接输入 A 并回车即可。
配置 Windows Terminal
源码
littleNewton
// 默认的配置就是我们的新 powershell(重要!!!)
"defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
{
// 键标记
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"name": "PowerShell Core 7.1.0.5",
"source": "Windows.Terminal.PowershellCore",
// 行为
"closeOnExit": true,
"commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo",
"hidden": false,
"historySize": 9001,
"snapOnInput": true,
"startingDirectory": ".",
// 外观
"icon": "D:/Users/newton/Documents/Softwares/software_windows/develop/shell/pwsh.ico",
"acrylicOpacity": 0.5,
"cursorColor": "#FFFFFF",
"cursorShape": "bar",
"fontFace": "Fira Code",
"fontSize": 11,
"padding": "5, 5, 20, 25",
"useAcrylic": false,
// 颜色主题
"colorScheme": "Homebrew"
},
or
小冰
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{9351e826-f1e7-4d53-b1bb-587cf839bd08}", // 这里设置为hexo的GUID,则默认打开hexo
"copyOnSelect": false,
"copyFormatting": false,
"profiles":
{
"defaults":
{ "fontFace": "Fira Code", // 这里设置字体
"useAcrylic":false, // 这里关掉亚克力效果才能使用背景图片
"backgroundImage" : "ms-appdata:///roaming/alex-nice-futuristic-city-alexnice.jpg", // 这里是背景图片
"backgroundImageOpacity" : 0.2, // 这里设置透明度
},
"list":
[
{ // 这里是系统自带的Windows PowerShell
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": true,// 设置隐藏
},
{ // 这里是我们用的hexo-PowerShell
"icon": "ms-appdata:///roaming/hexo.png", // 设置icon
"guid": "{9351e826-f1e7-4d53-b1bb-587cf839bd08}", // 设置GUID
"name": "hexo", // 设置名称
"commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo", // 设置程序地址,"-nologo"去除广告标题
"hidden": false,
"startingDirectory": "d:\\myblog\\" // 设置根目录地址,可以学我放D盘省事
},
{// 这里是命令提示符
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "命令提示符",
"commandline": "cmd.exe",
"hidden": false
},
{// "Azure Cloud Shell"
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": true,// 设置隐藏
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{ // 这里是安装的 PowerShell Core
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"name": "PowerShell",
"commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo", // "-nologo"去除广告标题
"source": "Windows.Terminal.PowershellCore"
}
]
},
Homebrew 配色
{
"name": "Homebrew",
"black": "#000000",
"red": "#FC5275",
"green": "#00a600",
"yellow": "#999900",
"blue": "#6666e9",
"purple": "#b200b2",
"cyan": "#00a6b2",
"white": "#bfbfbf",
"brightBlack": "#666666",
"brightRed": "#e50000",
"brightGreen": "#00d900",
"brightYellow": "#e5e500",
"brightBlue": "#0000ff",
"brightPurple": "#e500e5",
"brightCyan": "#00e5e5",
"brightWhite": "#e5e5e5",
"background": "#283033",
"foreground": "#00ff00"
},
添加右键菜单
{% btns rounded grid5 %}
{% cell 本站下载, https://dogecloud.yuqi.work/app/src.7z, fas fa-download %}
{% cell 蓝奏云下载, https://wwi.lanzouw.com/ig5azy4co5e, fas fa-download %}
{% endbtns %}
下载上面的脚本,然后在管理员模式的 powershell 7 里运行:
.\install.ps1 mini
添加 Powershell 启动参数
在 powershell 中输入
notepad.exe $Profile
紧接着在弹出的页面中输入下面这一长串代码,保存并关闭。这个 Profile 配置文件与 .zshrc / .bashrc 文件一样,都是控制启动前参数的。
<#
* FileName: Microsoft.PowerShell_profile.ps1
* Author: 刘 鹏
* Email: littleNewton6@outlook.com
* 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 -------------------------------
友情链接
本文素材来源于: 小冰博客、littleNewton(知乎博主)
评论区