序言:效果展示

每次在使用 hexo 时,总需要 CD 到博客目录下或者前往博客目录打开 PowerShell。

每次写博客都要这样操作,显得十分繁琐。

同时,每次看到丑陋的 PowerShell 界面,总提不起写作的兴趣。

这可不是拖更博客的理由哈哈哈,说白了还是懒。

为了解决这一痛点,我研究了 Windows Terminal,

利用 Windows Terminal 的设置美化了界面,

实现了直接打开 PowerShell 以 hexo 目录进行操作。

废话不多说,让我来教大家如何安装和设置吧。


步骤 1:下载&安装 Windows Terminal

必须运行 Windows 1903 (build >= 10.0.18362.0)或更高版本才能运行 Windows 终端

首先需要解决的是你的系统环境问题。如果你有经常升级系统的话,这个问题就不会给你造成影响。
如果你的 Windows 版本不够的话,可以使用windows 易升进行升级,这里不进行赘述。
打开你的 windows 应用商店(Microsoft Store)搜索 Windows Terminal 进行下载&安装。

安装完成后,在”C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_版本号_x64__xxxxxxxx”目录下,打开”WindowsTerminal.exe”即可打开软件。

右键创建快捷方式到桌面,或固定到任务栏中。


步骤 2:下载&安装 PowerShell Core

为了获取更佳的 PowerShell 体验,需下载 PowerShell core 版本。
你可以点击直接下载
你也可以前往 github 查看最新版本,并下载。
安装完成后,在”C:\Program Files\PowerShell\7-preview”目录下,打开”pwsh.exe”即可打开软件。


《Windows Terminal 完美配置 PowerShell 7.1》

步骤 3:配置 PowerShell Core

① 安装 powershell 插件

打开刚装好的新版 powershell,逐行输入以下三行命令,等待安装(可能会有点慢)。

后面两个包的来源可能不受系统信任,不用管它,如果让你选择是否信任,直接输入 Y 即可。

1
2
3
4
5
6
7
Install-Module -Name PSReadLine -AllowPrerelease -Force
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

# 1. 安装 PSReadline 包,该插件可以让命令行很好用
# 2. 安装 posh-git 包,让你的 git 更好用
# 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅

② 设置 powershell 启动参数

添加 Powershell 启动参数。在 powershell 中输入:
紧接着在弹出的页面中输入下面这一长串代码,保存并关闭。
#——————————- Import Modules BEGIN ——————————-

引入 posh-git

Import-Module posh-git

引入 oh-my-posh

Import-Module oh-my-posh

设置 PowerShell 主题

Set-Theme Paradox

#——————————- Import Modules END   ——————————-

#——————————-  Set Hot-keys BEGIN  ——————————-

设置 Tab 键补全

Set-PSReadlineKeyHandler -Key Tab -Function Complete

设置 Ctrl+d 为菜单补全和 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”

更新 pip 的方法

function Update-Packages {

update pip

Write-Host “Step 1: 更新 pip” -ForegroundColor Magenta -BackgroundColor Cyan

$a = pip list –outdated

$num_package = i = 0; $i -lt $num_package; $i++) {

a[2 + $i].Split(“ “))[0]

pip install -U $tmp

}

1
2
3
4
5
# 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

}

#——————————-    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

#——————————-    Set Alias END     ——————————-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

<hr></hr>

## 步骤4:配置 Windows Terminal ##



### ①安装代码美化字体 ###

<p>首先在系统安装字体,<a target="_blank" href="https://zhuanlan.zhihu.com/p/137595941">单击此处从 GitHub 下载</a>。</p>

### ②设置Windows Terminal参数 ###

![example](/images/shezhi.png)

<p>点击设置会弹出一个json文档,用txt打开。</p>

<p> 主要修改的设置如下:</p>

```json
{
"$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"
}
]
},

ps:图片请存放于”C:\Users\用户名\AppData\Local\Packages\Microsoft.WindowsTerminal_xxxxxxxx\RoamingState”里。
hexo icon 下载
这里的原理是将具有 hexo 初始路径的 PowerShell 程序作为默认打开窗口,当打开 Windows Terminal 即可直接进行 hexo 操作。
通过输入以下代码可打开博客目录:

1
start .

你也可以根据你的实际编码需求修改”defaultProfile”的 GUID。
GUID 唯一且不能重复,可以通过这个网站随机生成替换。

③ 设置 Windows Terminal 右键扩展

点击下载脚本,将脚本解压某个盘的根目录。如”d:”。

右键以管理员模式运行”C:\Program Files\PowerShell\7-preview”目录下的”pwsh.exe”,并依次输入以下命令:

1
2
3
4
5
6
7
# 以管理员模式运行

CD D:\Open_Windows_Terminal_Here-master\Open_Windows_Terminal_Here-master\src
# 转到脚本目录

.\install.ps1
# 安装脚本

ps:如果你修改了 profile,那么需要用 uninstall.ps1 脚本先清除右键菜单,然后重新 install 一遍。


总结

享受全新的编码体验吧!

若有问题请与我邮件联系499984532@qq.com