PowerShell-7.3.0-win-x64官方下载地址
https://github.com/PowerShell/PowerShell/releases/download/v7.3.0/PowerShell-7.3.0-win-x64.msi
1. 美化PowerShell
1.1 posh-git
以管理员身份运行PowerShell,在PowerShell面板中,以依次执行即可
posh-git地址
https://github.com/dahlbyk/posh-git
# 安装依赖包 posh-git 作用:美化命令行,特别是git
$ Install-Module posh-git -Force
# 引入依赖包 posh-git
$ Import-Module posh-git
# 安装依赖包 PSReadLine 作用:提供PowerShell命令的联想,rightArrow选中联想结果
$ Install-Module -Name PSReadLine -AllowClobber -Force
# 引入依赖包 PSReadLine
$ Import-Module PSReadLine
注:
- Oh My Posh 依赖包美化PowerShell命令行,太花式,不如简约的【posh-git】依赖包
- Oh My Posh:全平台终端提示符个性化工具文章,官方网站
1.2 Git 状态摘要信息
Git 状态摘要信息一目了然地提供了丰富的“Git 状态”信息,始终在您的 提示。
默认情况下,状态摘要具有以下格式:
[{HEAD-name} S +A ~B -C !D | +E ~F -G !H W]
{HEAD-name}
是当前分支,或分离的 HEAD 的 SHA
- 青色表示分支与其远程匹配
- 绿色表示分支在其远程之前(绿灯推动)
- 红色表示分支机构位于其远程后面
- 黄色表示分支在其远程的前面和后面
S
(status)表示与远程(跟踪的源)分支相关的分支状态
注: 此状态信息反映远程跟踪分支在最后一个远程分支之后的状态。执行以更新到默认远程存储库上的最新版本。如果您有多个遥控器, 执行。git fetch/pull
git fetch
git fetch --all
≡
= 本地分支与远程分支处于同一提交级别↑<num>
= 本地分支领先于远程分支的提交次数;需要push远程分支↓<num>
= 本地分支落后于远程分支的提交次数;需要pull本地分支<a>↕<b>
= 本地分支位于远程分支的提交记录 (a)和提交记录 (b)之间;在push远程分支之前,需要本地进行一次变基。x
= 本地分支跟踪到从远程被删除的分支
在 |
之前的ABCD,表示git在暂存区的变化,状态显示为深绿色
+
= 添加的文件~
= 修改的文件-
= 已删除的文件!
= 冲突的文件
在 |
之后的EFGHW,表示git在工作区的变化,状态显示为深红色
W表示工作目录的总体状态
!
= 工作树中存在未暂存的更改~
= 存在未提交的更改,即工作树中等待提交的阶段更改- 无 = 对工作树没有未暂存或未提交的更改
例如,状态对应于以下内容:
[main ≡ +0 ~2 -1 | +1 ~1 -0]
git status
# On branch main
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: this-changed.txt
# modified: this-too.txt
# deleted: gone.ps1
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: not-staged.ps1
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# new.file
2. 字体 Hack:贵族中的王者
Hack Nerd Font Mono
下载地址 https://gitee.com/Enjoy-li/public-assets/raw/master/tool/Hack-Nerd-Font.zip
易混淆的字符:
ao oO0QCG iIlL|1 g9q {}[] ~-+<=
注:字体名中的含有
- Mono,即Monospaced Font,一种等宽字体
- NL是无连字版本。
3. 操作命令–文件
- 项(item)可以是文件,也可以是文件夹
- powershell 命令不区分大小写,大小写只是为了便于阅读
- 可选参数[-Path]可以配合通配符
*?
使用,可选参数[-Recurse]表示递归处理,可选参数[-Force]表示目录不存在时,创建相应目录
操作 | PowerShell命令 | DOS命令(Cmd) |
---|---|---|
帮助命令 | get-help [command] | help [command] |
清除命令面板 | Alt + L(快捷键 ) | cls |
切换目录 | get-location [-Path] | cd [Path] |
获取项和子项(目录)(get-item * 获取当前目录) | get-childItem [-Path] [-Recurse] [-Force] [-Directory] [-File] [-Depth] [-Filter] | dir |
创建文件 | new-item [-Path] [-ItemType {directory|file}] [-Value] [-Force] | cd .>fileName.txt、md 目录名 |
内容追加到文件中(不在时,创建) | add-content [-Path] [-Value] [-Filter] [-Force] | echo 内容>>fileName.txt |
替换文件(新文件)中的现有内容 | set-content [-Path] [-Value] [-Filter] [-Force] | echo 内容>fileName.txt |
显示文件的内容 | get-content [-Path] [-Filter] [-Force] | type fileName.txt |
清空文件的内容 | clear-content [-Path] [-Filter] [-Force] | type nul>fileName.txt |
复制文件 | copy-item [-Path] [-Destination] [-Force] [-Filter] [-Recurse] | copy oldFileName newFileName |
移动文件(可重命名) | move-item [-Path] [-Destination] [-Force] [-Filter] | move oldFileName newFileName |
重命名(类似Move-Item,但无法修改路径) | rename-item [-Path] [-NewName] [-Force] | ren oldNameName newNameName |
(循环)删除指定项 | remove-item [-Path] [-Filter] [-Recurse] [-Force] | rd /s /q 盘符名:\文件夹名、del [fileName]、rm [dirName] |
获取剪贴板的内容 | get-clipboard [-Raw] | - |
清除回收站的内容 | clear-recycleBin [-Force] | - |
4.Oh My Posh
可以方便自定义主题样式,显示如下:
注意: 自定义主题,使用图标时很有可能导致终端光标错位,解决办法–更换图标。
首先,下载安装包exe文件地址 https://ohmyposh.dev/docs/installation/windows,
然后,安装后,powershell面板运行以下命令:
# 1.获取配置文件 D:\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
$PROFILE
# 如何找不到Microsoft.PowerShell_profile.ps1文件,就新建创建这个文件,格式如下:
# New-Item -Path "C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.Powershell_profile.ps1" -ItemType file -Force
最后,编辑Microsoft.PowerShell_profile.ps1文件
# 安装 posh-git 命令如下:
# $ Install-Module posh-git -Force
# 导入 posh-git
Import-Module posh-git
# 更新 posh-git 命令如下:
# Update-Module posh-git
# 安装 PSReadLine 命令如下:
# $ Install-Module -Name PSReadLine -AllowClobber -Force
# 导入 PSReadLine
Import-Module PSReadLine
# 导入 oh-my-posh
# Import-Module oh-my-posh
# 设置 oh-my-posh 主题 方式一 命令如下:
# $ Set-PoshPrompt -Theme Agnoster
# 设置 oh-my-posh 主题 方式二
# oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\di4am0nd.omp.json" | Invoke-Expression
# oh-my-posh init powershell --config 'C:\Program Files (x86)\oh-my-posh\themes\patriksvensson.omp.json' | Invoke-Expression
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\custom_theme.omp.json" | Invoke-Expression
# 重新载入该配置文件 命令如下:
# $ . $PROFILE
自定义custom_theme.omp.json文件如下:
// 自定义主题 教程地址 https://ohmyposh.dev/docs/configuration/overview
// custom_theme.omp.json
{
"blocks": [
{
"alignment": "left",
"segments": [
{
"foreground": "#c4265e",
"style": "powerline",
"template": "{{ if .WSL }}WSL at {{ end }}{{.Icon}} ",
"type": "os"
},
{
"foreground": "#cccccc",
"properties": {
"style": "full"
},
"style": "plain",
"template": "{{ .Path }} ",
"type": "path"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#88846f",
"style": "plain",
"template": "<#c4265e>├ </>{{ .UserName }} ",
"type": "session"
}
],
"type": "prompt"
},
{
"alignment": "left",
"segments": [
{
"foreground": "red",
"style": "plain",
"template": "| root ",
"type": "root"
},
{
"foreground": "#06A4CE",
"style": "powerline",
"template": "| \ue798 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} ",
"type": "dart"
},
{
"foreground": "#88846f",
"style": "powerline",
"template": " {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} ",
"type": "node"
},
{
"foreground": "#4584b6",
"properties": {
"display_mode": "context",
"fetch_virtual_env": true
},
"style": "plain",
"template": "| \ue235 {{ .Venv }} ",
"type": "python"
},
{
"foreground_templates": [
"{{if eq \"Charging\" .State.String}}#4caf50{{end}}",
"{{if eq \"Discharging\" .State.String}}#40c4ff{{end}}",
"{{if eq \"Full\" .State.String}}#ff0000{{end}}"
],
"invert_powerline": true,
"properties": {
"charged_icon": "\uf00d ",
"charging_icon": "\ue234 "
},
"style": "powerline",
"template": "| {{ if not .Error }}{{ .Icon }}{{ .Percentage }}{{ end }}{{ .Error }} \uf295 ",
"type": "battery"
},
{
"foreground": "#66d9ef",
"properties": {
"branch_icon": "שׂ ",
"fetch_status": true,
"fetch_upstream_icon": true
},
"style": "plain",
"template": "<#e2e22a>[</>{{ .HEAD }}{{ if .Staging.Changed }}<#00AA00> {{ .Staging.String }}</>{{ end }}{{ if .Working.Changed }}<#D75F00> {{ .Working.String }}</>{{ end }}<#e2e22a>] </>",
"type": "git"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#c4265e",
"foreground_templates": [
"{{ if gt .Code 0 }}red{{ end }}"
],
"properties": {
"always_enabled": true
},
"style": "powerline",
"template": "└▶ ",
"type": "exit"
}
],
"type": "prompt"
}
],
"version": 2
}