命令提示符和 powershell 的自动补全

设置自动补全来提高用命令行操作时的效率

💻 命令提示符

https://github.com/chrisant996/clink/releases

解压压缩包

将这里的 _default.. 改为 default.. 后运行 clink.bat

-

自动补全测试

输入一个 ipconfig 的命令,输入 ip 就有自动补全了,按一下右箭头键就自动补全

-

改动 clink.bat

start "Clink" cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%%clink_quiet_arg%"

改成

cmd.exe /s /k ""%~dpnx0" inject %clink_profile_arg%%clink_quiet_arg%"

终端内调用

-

🧷 Powershell

安装补全的包

需要管理员权限,提示禁止执行脚本的话需要先执行 set-ExecutionPolicy RemoteSigned

Install-Module PSReadLine

powershell 配置项

检测 profile 文件,并新建、编辑 profile 文件

if (Test-path $profile) {} else { New-item $profile } ; notepad $profile

改写 profile 文件

Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History

输入 c 就有了自动建议的 clear

-

安装 oh-my-posh ( 美化 powershell )

完整文档查看:https://ohmyposh.dev/docs

执行安装指令

Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))

-

powershell 配置项

检测 profile 文件,并新建、编辑 profile 文件

if (Test-path $profile) {} else { New-item $profile } ; notepad $profile

posh 写入启动项

oh-my-posh init pwsh | Invoke-Expression

配置主题:

  • https://ohmyposh.dev/docs/themes 下载一个主题
  • 放到 C:\Users\你的用户名 文件夹下
  • 改动配置 ~/pomin.omp.json 就是指是我的主题文件
oh-my-posh init pwsh --config ~/pomin.omp.json | Invoke-Expression

-

下载一些额外的包美化颜色

Install-Module Get-ChildItemColor
Install-Module WindowsConsoleFonts

内部调用

因为是改动的 profile 文件,所以 powershell 启动会自动加载

-

我的主题文件

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "type": "prompt",
      "alignment": "left",
      "segments": [
        {
          "type": "session",
          "style": "plain",
          "foreground": "#5cb85c",
          "properties": {
            "prefix": "",
            "postfix": "@Windows ",
            "template": "{{ .UserName }}"
          }
        },
        {
          "type": "path",
          "style": "plain",
          "foreground": "#56B6C2",
          "properties": {
            "style": "full",
            "prefix": "[",
            "postfix": "] "
          }
        },
        {
          "type": "git",
          "style": "plain",
          "foreground": "#D0666F",
          "properties": {
            "branch_icon": "",
            "display_status": false,
            "prefix": "<#5FAAE8>git(</>",
            "postfix": "<#5FAAE8>) </>"
          }
        },
        {
          "type": "exit",
          "style": "plain",
          "foreground": "#DCB977",
          "properties": {
            "prefix": "\u2717",
            "display_exit_code": false
          }
        }
      ]
    },
    {
      "type": "prompt",
      "alignment": "left",
      "newline": true,
      "segments": [
        {
          "type": "text",
          "style": "plain",
          "newline": true,
          "foreground": "#56B6C2",
          "properties": {
            "prefix": "",
            "text": "->"
          }
        }
      ]
    }
  ],
  "final_space": true
}

⚡⚡⚡ OVER ⚡⚡⚡