windows terminal加powershell7美化命令行操作

之前写过一篇文章vscode+wsl+windows terminal美化和搭建linux开发环境

博客链接

wsl挺好用的,不过比较麻烦,而且windows下的liunx子系统不完整,很多东西不能用,毕竟不是虚拟机。看到powershell7,心动了,本篇文章就如标题所言美化命令行操作。

下载windows terminal

Microsoft Store就有下载

下载并安装powershell7

选个最新的就行,我下载的是7.1.0-preview3,powershell7下载

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

也可以用powershell的Install-Module,不过还是推荐使用scoop或者chocolatey

scoop为例:

安装scoop

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

美化powershell

这里主要用到两个包,PSReadLine命令行提示记录,starship美化显示样式,colortool颜色配置

  1. 安装

    Install-Module -Name PSReadLine -AllowPrerelease -Force # PSReadLine
    scoop install colortool
    scoop install starship
    
  2. 写powershell的配置文件

    打开配置文件

    notepad.exe $Profile
    # or 用vscode
    code $Profile
    

    写入

    #-----------------------------------------------------PSReadline
    # 设置 Tab 键补全
    Set-PSReadlineKeyHandler -Key Tab -Function Complete
    
    # 设置 Ctrl+d 为菜单补全和 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
    
    # 一些PSReadline配置
    $PSReadLineOptions = @{
        EditMode = "Emacs"
        HistoryNoDuplicates = $true
        HistorySearchCursorMovesToEnd = $true
        Colors = @{
            # 提示文字颜色,原来的颜色太浅了,我这里换了
            Prediction = '#8F8B8B'
        }
    }
    Set-PSReadLineOption @PSReadLineOptions
    
    #-----------------------------------------------------starship
    Invoke-Expression (&starship init powershell)
    
  3. 配置starship

    创建这个文件夹并创建startship.toml

    mkdir -p ~/.config && touch ~/.config/starship.toml
    

    如果你创建失败,说明有这个.config这个文件夹,直接

    touch ~/.config/starship.toml
    
    notepad.exe ~/.config/starship.toml
    # or
    code ~/.config/starship.toml
    

    具体配置看官网,这里出现的小方框或者乱码是字体缺失,我这里用的是FiraCode Nerd Font字体,这个后面再说

    大概像这个样子

    image
    [character]      
    symbol = "➜"    
    
    [aws]
    symbol = " "
    
    [battery]
    full_symbol = ""
    charging_symbol = ""
    discharging_symbol = ""
    
    [conda]
    symbol = " "
    
    [docker]
    symbol = " "
    
    [elixir]
    symbol = " "
    
    [elm]
    symbol = " "
    
    [git_branch]
    symbol = " "
    
    [golang]
    symbol = " "
    
    [haskell]
    symbol = " "
    
    [hg_branch]
    symbol = " "
    
    [java]
    symbol = " "
    
    [julia]
    symbol = " "
    
    [memory_usage]
    symbol = " "
    
    [nix_shell]
    symbol = " "
    
    [nodejs]
    # symbol = " "
    symbol=" "
    
    [package]
    symbol = " "
    
    [php]
    symbol = " "
    
    [python]
    symbol = " "
    
    [ruby]
    symbol = " "
    
    [rust]
    symbol = " "
    
  4. 配置colortool

    colortool -s # 颜色表
    colortool  OneHalfDark.itermcolors # colortools后面跟主题名字就行
    
image

下载并配置FiraCode Nerd Font字体

  1. 下载FiraCode Nerd Font字体

    打开nerdfonts网站,滑到最下面找到FiraCode Nerd Font下载解压,全选&右键安装

image
  1. vscode中配置字体

    打开vscode配置文件,写入

     "editor.fontFamily": "FiraCode Nerd Font,Consolas, 'Courier New', monospace"
    

    这时候你打开starship.toml就可以看到缺失的字体了

  2. windows terminal中配置字体

    在后续windows terminal配置中见

配置windows terminal

{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    // "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "copyOnSelect": true,

    "profiles": {
        "defaults": {
            "startingDirectory": "./",
            "useAcrylic": true,
            "acrylicOpacity": 0.8
        },
        "list": [{
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                "hidden": false,
                "useAcrylic": true
            },
            {
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "cmd",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": false,
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            // powershell 7
            {
                "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                "hidden": false,
                "name": "PowerShell",
                "commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo",
                "fontSize": 12,
                // 字体配置
                "fontFace": "FiraCode Nerd Font",
                "historySize": 9001,
                "padding": "5, 5, 20, 25",
                "snapOnInput": true,
                "colorScheme": "Andromeda",
                "source": "Windows.Terminal.PowershellCore"
            }
        ]
    },
    "schemes": [ {
        "name": "Andromeda",
        "black": "#000000",
        "red": "#cd3131",
        "green": "#05bc79",
        "yellow": "#e5e512",
        "blue": "#2472c8",
        "purple": "#bc3fbc",
        "cyan": "#0fa8cd",
        "white": "#e5e5e5",
        "brightBlack": "#666666",
        "brightRed": "#cd3131",
        "brightGreen": "#05bc79",
        "brightYellow": "#e5e512",
        "brightBlue": "#2472c8",
        "brightPurple": "#bc3fbc",
        "brightCyan": "#0fa8cd",
        "brightWhite": "#e5e5e5",
        "background": "#262a33",
        "foreground": "#e5e5e5"
    }],
    "keybindings": []
}

这个时候打开windows terminal

image

windows terminal 加入系统右键

win+r输入regedit找到计算机\HKEY_CLASSES_ROOT\Directory\Background\shell
新建项Terminal(名字随便取)

image
image
  1. 选中Terminal(默认)为写入右键的文本

  2. Icon为右键图标,自己找一个ico

  3. 右键Terminal新建项command,在默认下写入值C:\Users\niwen\AppData\Local\Microsoft\WindowsApps\wt.exe

    这里的niwen是我电脑用户的名称,你们自己找到wt.exe然后把地址写上去就行

ok搞定现在试一下右键点击Terminal Here

image

发表评论

电子邮件地址不会被公开。 必填项已用*标注