windows terminal使用powershell7和美化记录

效果图

DnNg8U.png
DnNg8U.png

下载powershell7

点击这里下载powershell7,选择PowerShell-x.x.x-win-x64.msi下载

下载scoop或者 chocolatey (windows包管理工具)

安装scoop

1
2
3
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
# or
iwr -useb get.scoop.sh | iex

安装包

这里主要用到两个包,PSReadLine命令行提示记录,starship美化显示样式,colortool颜色配置,posh-git是让命令行能够像git一样的使用体验,oh-my-posh可以设置powershell的主题

1
2
3
4
5
6
7
8
Install-Module Get-ChildItemColor -AllowClobber
Install-Module -Name PSReadLine -AllowPrerelease -Force # PSReadLine
scoop install colortool
scoop install starship
Install-Module git-aliases -AllowClobber
Install-Module posh-git
Install-Module oh-my-posh #
Install-Module DirColors #让 ls (Get-ChildItem) 像 Unix 系终端一样具有多彩色

安装PSReadLine之前先安装PowerShellGet,使用Install-Module -Name PowerShellGet -Force

如果下载慢可以使用代理,在代理软件设置允许局域网访问,然后在powershell7命令行输入以下命令

1
2
$env:http_proxy="127.0.0.1:1080"
$env:https_proxy="127.0.0.1:1080"

安装字体

这个网站选择一款字体下载,全选右键安装

我在这里使用的是JetBrains Mono字体,可以在这里下载

创建$profile文件

打开powershell7命令行,输入一下命令

1
2
3
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}

然后使用$profile查看路径,如果配置成功则会显示刚才设置的路径

具体操作可查看官方的文档

设置terminal

iTem2-Color-Schemes找主题颜色
在settting.json输入一下设置

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// This file was initially generated by Windows Terminal 1.0.1811.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",

"defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",

// You can add more global application settings here.
// To learn more about global settings, visit https://aka.ms/terminal-global-settings

// If enabled, selections are automatically copied to your clipboard.
"copyOnSelect": false,

// If enabled, formatted data is also copied to your clipboard
"copyFormatting": false,

// 使用系统的暗黑主题
"theme": "dark",

// A profile specifies a command to execute paired with information about how it should look and feel.
// Each one of them will appear in the 'New Tab' dropdown,
// and can be invoked from the commandline with `wt.exe -p xxx`
// To learn more about profiles, visit https://aka.ms/terminal-profile-settings
"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles.
},
"list":
[
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe --nologo", // -nologo不显示欢迎信息
"colorScheme": "Atom",
"hidden": false
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "命令提示符",
"commandline": "cmd.exe",
"hidden": false
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"name": "powershell",
"useAcrylic" : true, // 是否使用毛玻璃
"acrylicOpacity": 0.8, // 毛玻璃的透明度
"backgroundImage": "", // 背景图
"backgroundImageOpacity": 0.3, // 背景图的透明度
"historySize": 9001,
"padding": "5, 5, 20, 25",
"fontFace": "JetBrains Mono",
"fontSize": 12,
"colorScheme": "Molokai", // 设置的主题名
"source": "Windows.Terminal.PowershellCore"
},
]
},

// Add custom color schemes to this array.
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
"name": "Molokai",
"black": "#121212",
"red": "#fa2573",
"green": "#98e123",
"yellow": "#dfd460",
"blue": "#1080d0",
"purple": "#8700ff",
"cyan": "#43a8d0",
"white": "#bbbbbb",
"brightBlack": "#555555",
"brightRed": "#f6669d",
"brightGreen": "#b1e05f",
"brightYellow": "#fff26d",
"brightBlue": "#00afff",
"brightPurple": "#af87ff",
"brightCyan": "#51ceff",
"brightWhite": "#ffffff",
"background": "#121212",
"foreground": "#bbbbbb"
}
],

// Add custom keybindings to this array.
// To unbind a key combination from your defaults.json, set the command to "unbound".
// To learn more about keybindings, visit https://aka.ms/terminal-keybindings
"keybindings":
[
// Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
// These two lines additionally bind them to Ctrl+C and Ctrl+V.
// To learn more about selection, visit https://aka.ms/terminal-selection
{ "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
{ "command": "paste", "keys": "ctrl+v" },

// Press Ctrl+Shift+F to open the search box
{ "command": "find", "keys": "ctrl+shift+f" },

// Press Alt+Shift+D to open a new pane.
// - "split": "auto" makes this pane open in the direction that provides the most surface area.
// - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
// To learn more about panes, visit https://aka.ms/terminal-panes
{ "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
]
}

这个的guid要自己生成,在这里生成

配置$profile文件

打开powershell7命令行输入以下命令

1
notepad.exe $profile

如果你安装了notepad,notepad.exe命令才有用,如果没安装,就使用notepad $profile
在配置文件输入以下内容

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
Import-Module Get-ChildItemColor

$env:PYTHONIOENCODING="utf-8"
# Remove curl alias
If (Test-Path Alias:curl) {Remove-Item Alias:curl}
If (Test-Path Alias:curl) {Remove-Item Alias:curl}
# Remove-Item alias:ls -force
Set-Alias l Get-ChildItemColor -option AllScope
Set-Alias ls Get-ChildItemColorFormatWide -option AllScope

function GitLogPretty {
git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --all
}

function PrettyLS {
colorls --light -A
}

function GitStat {git status}

function GoBack {Set-Location ..}

function GetMyIp {curl -L tool.lu/ip}
function UpdateScoop {scoop update; scoop update *}

function UpdateChoco {choco upgrade chocolatey}

Import-Module posh-git
Import-Module oh-my-posh
Import-Module DirColors
# $DefaultUser = 'spenc'

# Setup other alias
Set-Alias open Invoke-Item
Set-Alias .. GoBack
Set-Alias glola GitLogPretty
Set-Alias gst GitStat
Set-Alias myip GetMyIp
Set-Alias pls PrettyLS

$PSReadLineOptions = @{
EditMode = "Emacs"
HistoryNoDuplicates = $true
HistorySearchCursorMovesToEnd = $true
# Colors = @{
# 提示文字颜色,原来的颜色太浅了,我这里换了
# "Prediction" = '#8F8B8B'
# }
}
Set-PSReadLineOption @PSReadLineOptions

# Set theme
Set-Theme robbyrussell

Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

保存,重新打开powershell,完成