PowerShell 美化及操作命令


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

注:

  1. Oh My Posh 依赖包美化PowerShell命令行,太花式,不如简约的【posh-git】依赖包
  2. Oh My Posh:全平台终端提示符个性化工具文章,官方网站

1.2 Git 状态摘要信息

请添加图片描述

Git 状态摘要信息一目了然地提供了丰富的“Git 状态”信息,始终在您的 提示。

默认情况下,状态摘要具有以下格式:

[{HEAD-name} S +A ~B -C !D | +E ~F -G !H W]

{HEAD-name}是当前分支,或分离的 HEAD 的 SHA

  1. 青色表示分支与其远程匹配
  2. 绿色表示分支在其远程之前(绿灯推动)
  3. 红色表示分支机构位于其远程后面
  4. 黄色表示分支在其远程的前面和后面

S(status)表示与远程(跟踪的源)分支相关的分支状态

: 此状态信息反映远程跟踪分支在最后一个远程分支之后的状态。执行以更新到默认远程存储库上的最新版本。如果您有多个遥控器, 执行。git fetch/pull git fetch git fetch --all

  1. = 本地分支与远程分支处于同一提交级别
  2. ↑<num> = 本地分支领先于远程分支的提交次数;需要push远程分支
  3. ↓<num> = 本地分支落后于远程分支的提交次数;需要pull本地分支
  4. <a>↕<b> = 本地分支位于远程分支的提交记录 (a)和提交记录 (b)之间;在push远程分支之前,需要本地进行一次变基。
  5. x= 本地分支跟踪到从远程被删除的分支

| 之前的ABCD,表示git在暂存区的变化,状态显示为深绿色

  1. += 添加的文件
  2. ~= 修改的文件
  3. -= 已删除的文件
  4. != 冲突的文件

| 之后的EFGHW,表示git在工作区的变化,状态显示为深红色

W表示工作目录的总体状态

  1. != 工作树中存在未暂存的更改
  2. ~= 存在未提交的更改,即工作树中等待提交的阶段更改
  3. 无 = 对工作树没有未暂存或未提交的更改

例如,状态对应于以下内容:

[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  {}[] ~-+<=

请添加图片描述

:字体名中的含有

  1. Mono,即Monospaced Font,一种等宽字体
  2. NL是无连字版本。

3. 操作命令–文件

微软powershell文档地址

https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/add-content?view=powershell-7.3

  1. 项(item)可以是文件,也可以是文件夹
  2. powershell 命令不区分大小写,大小写只是为了便于阅读
  3. 可选参数[-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
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值