强化win10下的命令行 - 颜值和实用并存

强化win10下的命令行 - 颜值和实用并存

更新时间:2021-01-03 20:18:15

  • 2021-10-21 10:40:57

    • 增加 PSReadLine 插件
  • 2021-01-04 10:49:08

    • 抄了更多小马哥的文章!!!
  • 2021-01-03 20:18:15

0x1 安装 Windows Terminal

Microsoft Store 中下载 Windows Terminal 注意系统要求

0x2 安装 PowerShell core

下载地址:https://github.com/PowerShell/PowerShell/releases

win10选择 PowerShell-7.1.0-preview.7-win-x64.msi 这种安装即可

0x3 安装字体(可选)

FiraCode - 连字符字体

JetBrainsMono - IDEA 系字体

更纱黑体Sarasa-Gothic

0x4 安装 PowerShell 模块

通过在 PowerShell 中执行下面的命令安装, 以超级管理员运行 PowerShell

右键桌面空白的地方选择 PowerShell > Open Here as Administrator

CurrentUser 是仅为当前用户安装模块

安装过程中加上 -Verbose 可以看到输出

注意事项

如果出现 PowerShell 因为在此系统上禁止运行脚本…

# 更改执行策略
set-ExecutionPolicy RemoteSigned

# 查看执行策略
get-ExecutionPolicy

如果在安装过程中遇到类似于这样的提示

不受信任的存储库你正在从不受信任的存储库安装模块。如果你信任该存储库,请通过运行 Set-PSRepositorycmdlet 更改其 InstallationPolicy 值。是否确实要从“PSGallery”安装模块?[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助

你可以按 Y 或 A 键,但是如果你觉得每次都这样麻烦的话,可以先执行下面的命令:

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

之后再安装模块就不会出现这个提示了。

如果下载慢的话可以使用命令行代理

命令行下载依旧很慢的可以试试全局科学上网!!!

# 设置代理(填写你本地的端口)
netsh winhttp set proxy 127.0.0.1:1080
# 取消代理
netsh winhttp reset proxy
# 查看代理
netsh winhttp show proxy
# clash
$Env:http_proxy="http://127.0.0.1:7890";$Env:https_proxy="http://127.0.0.1:7890"

参数说明

# -Scope CurrentUser 安装在当前用户作用域下
Install-Module posh-git -Scope CurrentUser
# -Verbose 输出日志
Install-Module posh-git -Scope CurrentUser -Verbose

posh-git

Install-Module posh-git

oh-my-posh

Install-Module oh-my-posh

PSColor

在默认情况下, PowerShell 的文件列表并不会彩色显示。

想要文件列表彩色显示的话,最简单的方法就是安装一个 PowerShell 模块:PSColor

这个模块安装使用都很简单,打开 Windows PowerShell 管理员控制台,输入:

Install-Module PSColor

DirColors

Install-Module DirColors

哪些文件类型可以被加亮显示是可以配置的,在 PSColor 官方的 README 中有介绍,这里就不转述了。不过这个配置方式是 PowerShell 式的,如果能直接像上面使用 itermcolors 文件配置控制台色彩一样,直接用 Linux 平台上的现成的 dircolors 配置文件的话,会不会更方便呢?这个想法很好,而且还真的有人实现了,它就是 DirColors

这也是一个 PowerShell 模块,安装方式跟 PSColor 一样,使用:

Install-Module DirColors

导入该模块。接下来,如果你想要载入某个现成的 dircolors 配置文件的话,只需要用:

Update-DirColors ~\dir_colors

这条命令就可以了。

其中 ~\dir_colors 就是配置文件的路径,关于 dir_colors 的配置文件,在 github 上可以搜到不少,比如:dircolors-solarized。这里就不再列举更多了。

PSReadLine

如果是使用自带的 powershell,先执行

Install-Module -Name PowerShellGet -Force
Exit

然后安装

Install-Module PSReadLine -AllowPrerelease -Force
# 稳定版本
Install-Module PSReadLine

0x5 配置 PowerShell

字体配置

模块配置

0x1 输入:

$PROFILE

# C:\Users\SunSeekerX\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

0x2 继续输入:

if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE

0x3 在打开的文件中添加Set-PoshPrompt Paradox 是设置默认主题

以前为 Set-Theme

该文件是每次启动 PowerShell 执行的文件,加载两个模块和设置主题。

其他可选主题有

Agnoster、Avit、Darkblood、Fish、Honukai、Paradox、Sorin、tehrob, PowerLine

# Import Modules BEGIN
# 引入 DirColors
Import-Module DirColors

# 引入 posh-git
Import-Module posh-git

# 引入 oh-my-posh
Import-Module oh-my-posh

# 引入 ps-read-line
Import-Module PSReadLine

# 设置 PowerShell 主题
Set-PoshPrompt 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 

0x4 保存后关闭记事本, 在终端中输入以下命令生效(这里不要照抄 下面执行的 是 上文中$PROFILE的值)

C:\Users\SunSeekerX\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

0x5 切换主题

Set-Theme $主题名字

0x6 配置 Windows Terminal

官网详细配置说明

配置主题配色

0x1 打开配置文件 setting.json

0x2 参考以下配置

可自行修改,一定要符合json的内容格式,不然会报错,还有就是图片的的路径要与己系统对应

改的比较多的就是 profiles > defaultsschemes ,前者是启动的配置,后者是主题配色。

下面的配置不要全部复制,可以选择性的更改你的配置!

{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions": [
        {
            "command": {
                "action": "copy",
                "singleLine": false
            },
            "keys": "ctrl+c"
        },
        {
            "command": "find",
            "keys": "ctrl+shift+f"
        },
        {
            "command": "paste",
            "keys": "ctrl+v"
        },
        {
            "command": {
                "action": "splitPane",
                "split": "auto",
                "splitMode": "duplicate"
            },
            "keys": "alt+shift+d"
        }
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "profiles": {
        "defaults": {
            "colorScheme": "One Half Dark",
            "font": {
                "face": "Sarasa Term SC",
                "size": 12
            },
            "acrylicOpacity": 0.7,
            "useAcrylic": true,
            "background": "#242425"
        },
        "list": [
            {
                "commandline": "powershell.exe",
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell"
            },
            {
                "commandline": "cmd.exe",
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "\u547d\u4ee4\u63d0\u793a\u7b26"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            }
        ]
    },
    "schemes": [
        {
            "background": "#262A33",
            "black": "#000000",
            "blue": "#2472C8",
            "brightBlack": "#666666",
            "brightBlue": "#2472C8",
            "brightCyan": "#0FA8CD",
            "brightGreen": "#05BC79",
            "brightPurple": "#BC3FBC",
            "brightRed": "#CD3131",
            "brightWhite": "#E5E5E5",
            "brightYellow": "#09DA0F",
            "cursorColor": "#FFFFFF",
            "cyan": "#0FA8CD",
            "foreground": "#E5E5E5",
            "green": "#05BC79",
            "name": "Andromeda",
            "purple": "#BC3FBC",
            "red": "#CD3131",
            "selectionBackground": "#FFFFFF",
            "white": "#E5E5E5",
            "yellow": "#09DA0F"
        },
        {
            "background": "#0A0E14",
            "black": "#01060E",
            "blue": "#53BDFA",
            "brightBlack": "#686868",
            "brightBlue": "#59C2FF",
            "brightCyan": "#95E6CB",
            "brightGreen": "#C2D94C",
            "brightPurple": "#FFEE99",
            "brightRed": "#F07178",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#FFB454",
            "cursorColor": "#FFFFFF",
            "cyan": "#90E1C6",
            "foreground": "#B3B1AD",
            "green": "#91B362",
            "name": "Ayu Dark",
            "purple": "#FAE994",
            "red": "#EA6C73",
            "selectionBackground": "#FFFFFF",
            "white": "#C7C7C7",
            "yellow": "#F9AF4F"
        },
        {
            "background": "#FAFAFA",
            "black": "#000000",
            "blue": "#3199E1",
            "brightBlack": "#686868",
            "brightBlue": "#399EE6",
            "brightCyan": "#4CBF99",
            "brightGreen": "#86B300",
            "brightPurple": "#A37ACC",
            "brightRed": "#F07171",
            "brightWhite": "#D1D1D1",
            "brightYellow": "#F2AE49",
            "cursorColor": "#FFFFFF",
            "cyan": "#46BA94",
            "foreground": "#6C7680",
            "green": "#99BF4D",
            "name": "Ayu Light",
            "purple": "#9E75C7",
            "red": "#EA6C6D",
            "selectionBackground": "#FFFFFF",
            "white": "#C7C7C7",
            "yellow": "#ECA944"
        },
        {
            "background": "#1F2430",
            "black": "#191E2A",
            "blue": "#6DCBFA",
            "brightBlack": "#686868",
            "brightBlue": "#73D0FF",
            "brightCyan": "#95E6CB",
            "brightGreen": "#BAE67E",
            "brightPurple": "#D4BFFF",
            "brightRed": "#F28779",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#FFD580",
            "cursorColor": "#FFFFFF",
            "cyan": "#90E1C6",
            "foreground": "#CBCCC6",
            "green": "#A6CC70",
            "name": "Ayu Mirage",
            "purple": "#CFBAFA",
            "red": "#ED8274",
            "selectionBackground": "#FFFFFF",
            "white": "#C7C7C7",
            "yellow": "#FAD07B"
        },
        {
            "background": "#0C0C0C",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#F2F2F2",
            "green": "#13A10E",
            "name": "Campbell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#012456",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#CCCCCC",
            "green": "#13A10E",
            "name": "Campbell Powershell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#282C34",
            "black": "#282C34",
            "blue": "#61AFEF",
            "brightBlack": "#5A6374",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B6C2",
            "brightGreen": "#98C379",
            "brightPurple": "#C678DD",
            "brightRed": "#E06C75",
            "brightWhite": "#DCDFE4",
            "brightYellow": "#E5C07B",
            "cursorColor": "#FFFFFF",
            "cyan": "#56B6C2",
            "foreground": "#DCDFE4",
            "green": "#98C379",
            "name": "One Half Dark",
            "purple": "#C678DD",
            "red": "#E06C75",
            "selectionBackground": "#FFFFFF",
            "white": "#DCDFE4",
            "yellow": "#E5C07B"
        },
        {
            "background": "#FAFAFA",
            "black": "#383A42",
            "blue": "#0184BC",
            "brightBlack": "#4F525D",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B5C1",
            "brightGreen": "#98C379",
            "brightPurple": "#C577DD",
            "brightRed": "#DF6C75",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#E4C07A",
            "cursorColor": "#4F525D",
            "cyan": "#0997B3",
            "foreground": "#383A42",
            "green": "#50A14F",
            "name": "One Half Light",
            "purple": "#A626A4",
            "red": "#E45649",
            "selectionBackground": "#FFFFFF",
            "white": "#FAFAFA",
            "yellow": "#C18301"
        },
        {
            "background": "#073642",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#FFFFFF",
            "cyan": "#2AA198",
            "foreground": "#FDF6E3",
            "green": "#859900",
            "name": "Solarized Dark",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#FFFFFF",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#FDF6E3",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#002B36",
            "cyan": "#2AA198",
            "foreground": "#657B83",
            "green": "#859900",
            "name": "Solarized Light",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#FFFFFF",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#FFFFFF",
            "cyan": "#06989A",
            "foreground": "#D3D7CF",
            "green": "#4E9A06",
            "name": "Tango Dark",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#FFFFFF",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#FFFFFF",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#000000",
            "cyan": "#06989A",
            "foreground": "#555753",
            "green": "#4E9A06",
            "name": "Tango Light",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#FFFFFF",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#000080",
            "brightBlack": "#808080",
            "brightBlue": "#0000FF",
            "brightCyan": "#00FFFF",
            "brightGreen": "#00FF00",
            "brightPurple": "#FF00FF",
            "brightRed": "#FF0000",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#FFFF00",
            "cursorColor": "#FFFFFF",
            "cyan": "#008080",
            "foreground": "#C0C0C0",
            "green": "#008000",
            "name": "Vintage",
            "purple": "#800080",
            "red": "#800000",
            "selectionBackground": "#FFFFFF",
            "white": "#C0C0C0",
            "yellow": "#808000"
        }
    ]
}

0x3 官方更多配色

https://docs.microsoft.com/zh-cn/windows/terminal/customize-settings/color-schemes

添加 Windows Terminal 到右键

现在好像会自动添加到右键了。

0x1 下载图标

下载地址:icon,放到 C:\Users\[user_name]\AppData\Local\terminal,没有这个文件夹新建一个。

注意[user_name] 是你自己电脑的用户名

0x2 写入注册表

创建一个txt文档,并把档后缀改为 reg。文档的名字可自己创建,后缀名不可以错。右键菜单出现 Windows Terminal 有两种方法。一种是按shift+ 右键,另一种是直接右键

shift + 右键

把下面的内容复制到reg去

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Windows Terminal"
"Icon"="%USERPROFILE%\\AppData\\Local\\terminal\\wt_32.ico"
"Extended"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\[user_name]\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

注意:请把 [user_name] 改成自己电脑的用户名

右键

把下面的内容复制到reg去

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Windows terminal here"
"Icon"="%USERPROFILE%\\AppData\\Local\\terminal\\wt_32.ico"

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\[user_name]\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

注意:请把 [user_name] 改成自己电脑的用户名

0x3 修改 Windows Terminal 的 settings.json

profiles > list > startingDirectory 改为 null,没有的自己创建一个。

可以选择的改你需要的。(我都改了)

这个是修改右键启动路径为当前运行命令的路径。

加入下面这个在启动的时候就不会有logo打印了,就是诸如 加载个人文件花费了多少毫秒的提示

"terminal.integrated.shellArgs.windows": ["-NoLogo", "-NoExit", "-Command", "& { Write-Host }"]

0x7 结语

平时工作中命令行 git 命令用的比较多,安装了扩展,输入命令可以按 Tab 来自动补全方便了很多。

Windows 下的命令行还有很多的玩法,比如支持 Linux 命令的 MSYS2,集成了 pacman。 可以参考 Win10终端神器——Windows Terminal 与 MSYS2 MinGW64 集成记 进行安装。

0x8 参考链接

关于我

SunSeekerX,前端开发、Nodejs开发、小程序、uni-app开发、等等

喜欢探讨技术实现方案和细节,完美主义者,见不得bug

Github:https://github.com/SunSeekerX

个人博客:https://yoouu.cn/

个人在线笔记:https://sunseekerx.yoouu.cn/