1080 字
5 分钟

Terminal引入PowerShell

2022-05-03
浏览量 加载中...

1、安装Terminal#

在 Microsoft Store 搜索下载安装即可

2、安装字体#

打开网址:https://www.nerdfonts.com/ 下载JetBrainsMono Nerd Font或者FiraCodeNerd Font安装,推荐第一个。

3、安装Powershell#

打开网址:https://github.com/PowerShell/PowerShell/releases 从release里选个最新的preview版本,这些预览版都相当稳定,下载x64 Windows 64位.msi安装包进行安装

4、安装Powershell插件#

Terminal window
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

5、配置Terminal#

打开Windows Terminal,进入设置,找到配置文件,新建配置文件,名字为PowerShell Core,命令行为刚才安装的Powershell文件夹下的/pwsh.exe,然后保存。 然后在设置里点击打开JSON文件,可以找到刚才的配置文件,将默认defaultProfile的guid设置为它。 例如: "defaultProfile": "{900df208-c992-4ad2-a391-2c30d4d8fd7c}", 然后编辑配置文档:

{
//键标记
"guid": "{900df208-c992-4ad2-a391-2c30d4d8fd7c}",
"name": "PowerShell Core",
"source": "Windows.Terminal.PowershellCore",
// 行为
"closeOnExit": true,
"commandline": "D:/Programs/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": "JetBrainsMono Nerd Font Mono",
"fontSize": 11,
"padding": "5, 5, 20, 25",
"useAcrylic": false,
// 颜色主题
"colorScheme": "Homebrew"
},
然后修改`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"
},

6、添加右键菜单#

打开网址:https://github.com/LittleNewton/Open_Windows_Terminal_Here 将文件打包下载下来并解压,然后在src目录下以管理员模式打开powershell 7 执行命令:.\install.ps1 mini 如果修改过profile,那么需要用 uninstall.ps1 脚本先清除右键菜单,然后重新 install 一遍。

7、添加启动参数#

在 powershell 中输入notepad.exe $Profile 紧接着在弹出的页面中输入下面这一长串代码,保存并关闭。这个 Profile 配置文件与 .zshrc / .bashrc 文件一样,都是控制启动前参数的。

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

8、解决右键打开Terminal主题中断问题#

右键打开Terminal时主题样式会出现中断,应该是有未加载的样式配置,目前没找到很好地解决办法,但是只需要一直使用管理员身份运行就可以避免这个bug,还有一种曲线救国方法就是在上面配置文件中加入clear命令。

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Terminal引入PowerShell
https://blog.zwfpro.com/posts/terminal/
作者
赵公子
发布于
2022-05-03
许可协议
CC BY-NC-SA 4.0
最后更新于 2022-05-03,距今已过 1632 天

部分内容可能已过时

评论区

Profile Image of the Author
赵公子
所见皆可记,所遇皆有意
公告
欢迎来到岁语集,这里是赵公子的博客,记录技术、生活与思考。
分类
标签
站点统计
文章
14
分类
7
标签
24
总字数
6,151
运行时长
0 天
最后活动
0 天前

目录