設(shè)置Powershell終端

Intro

提到shell,似乎大家首先想到Linux,其實Windows的Powershell也是非常好的命令行界面。同時由于Powershell默認(rèn)狀態(tài)下已經(jīng)把很多常用的基礎(chǔ)bash命令(如cdls)都設(shè)置成了別名,因而對于一些常用操作基本上可以當(dāng)成bash來用。當(dāng)然還有一些缺失,比如whichtouch等命令,則需要通過自定義函數(shù)利用Powershell命令實現(xiàn)。另一方面,Powershell的藍(lán)色背景白色字符實在是缺乏美感,也影響日常使用,因而需要選擇一個終端并進(jìn)行美化工作。因而本文首先介紹幾個自定義的常用函數(shù)的方法,同時進(jìn)一步介紹使用oh-my-posh和Windows Terminal構(gòu)建美觀的Powershell終端。

常用bash命令實現(xiàn)

由于powershell自帶的別名有缺失或默認(rèn)功能與bash命令有所不同,因而需要自定義和重載一些函數(shù)。這些函數(shù)需要在profile.ps1中定義,這樣每次打開powershell時即可自動加載。以下介紹3個常用命令。

ls

ls時Powershell自帶的別名,然而其輸出的結(jié)果默認(rèn)為完整信息形式且沒有用不同顏色表示文件夾或文件。

image

這里使用Get-ChildItemColor實現(xiàn)不同文件類型以不同顏色顯示,同時增加不同別名來實現(xiàn)僅列出文件名和列出詳細(xì)文件信息功能。

該Cmdlet可以直接從Powershell Gellery中安裝


Install-Module -Name Get-ChildItemColor -Scope CurrentUser

profile.ps1中加入:

# Ensure that Get-ChildItemColor is loaded
Import-Module Get-ChildItemColor
# Set l and ls alias to use the new Get-ChildItemColor cmdlets
Set-Alias l Get-ChildItemColor -Option AllScope
Set-Alias ls Get-ChildItemColorFormatWide -Option AllScope

進(jìn)而實現(xiàn)使用ls列出文件名,使用l列出詳細(xì)列表。

image

touch

Powershell默認(rèn)沒有定義touch命令的別名,這里使用以下函數(shù)使用New-Item實現(xiàn)touch。


function touch($name)
{
    if ($name) {
        $file_path = Split-Path -Path $name
        $file_name = Split-Path -Path $name -Leaf
        if ($file_path -eq "") {
            $file_path = "."
        }
        if (-Not (Test-Path($file_path))) {
            New-Item -ItemType "directory" -Path $file_path
        }
        New-Item -Path $file_path -Name $file_name -ItemType "file"
    }
    else {
        Write-Host "Command to create new file."
    }
}

which

Powershell默認(rèn)也沒有定義which,這里用Get-Command實現(xiàn)which。

function which($name)
{
    Get-Command $name | Select-Object -ExpandProperty Definition
}

~

Powershell沒有使用~來代表用戶目錄,可以通過簡單的Set-Alias來實現(xiàn)。

function cuserprofile { Set-Location ~ }
Set-Alias ~ cuserprofile -Option AllScope

終端美化

終端美化有兩個目標(biāo),一是配色及字體,二是Prompt的主題,且支持Git repo的狀態(tài)指示。

Windows Terminal

默認(rèn)的Powershell終端支持有限的顏色,自定義能力極其有限,因而這里選用2019年發(fā)布的Windows Terminal來作為終端模擬器。

Windows Terminal可以從Microsoft Store安裝。

https://www.microsoft.com/en-us/p/windows-terminal-preview/9n0dx20hk701

Windows Terminal中的Powershell:


image

點擊Windows Terminal的Settings會直接打開profiles.json文件。在其中可以進(jìn)行各種個性化設(shè)置。以下是修改后的效果以及其對應(yīng)的json配置文件。

image
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation

{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles
        },
        "list":
        [
            {
                // Make changes here to the powershell.exe profile
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                "hidden": false,
                "colorScheme": "Cobalt2",
                "fontFace": "Cascadia Code PL",
                "background": "#000000",
                "useAcrylic": true,
                "acrylicOpacity": 0.8,
                "fontSize": 10,
                "backgroundImageStretchMode": "none",
                "backgroundImageOpacity": 0.1,
                "backgroundImageAlignment": "bottomRight"

            },
            {
                // Make changes here to the cmd.exe profile
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "cmd",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            }
        ]
    },

    // Add custom color schemes to this array
    "schemes": [
        {
            "name": "Cobalt2",
            "black": "#000000",
            "red": "#ff0000",
            "green": "#38de21",
            "yellow": "#ffe50a",
            "blue": "#1460d2",
            "purple": "#ff005d",
            "cyan": "#00bbbb",
            "white": "#bbbbbb",
            "brightBlack": "#555555",
            "brightRed": "#f40e17",
            "brightGreen": "#3bd01d",
            "brightYellow": "#edc809",
            "brightBlue": "#5555ff",
            "brightPurple": "#ff55ff",
            "brightCyan": "#6ae3fa",
            "brightWhite": "#ffffff",
            "background": "#132738",
            "foreground": "#ffffff"
        }
    ],

    // Add any keybinding overrides to this array.
    // To unbind a default keybinding, set the command to "unbound"
    "keybindings": []
}

其中,這里選用的配色主題是Cobalt2,它是iTerm Color Schemes中的一個主題,更多適用于Windows Terminal的配色可以從這里下載:
https://github.com/mbadolato/iTerm2-Color-Schemes/tree/master/windowsterminal。
只需要作為字典加入"schemes"這里列表里就行了。

這里字體使用了配合Windows Terminal發(fā)布的Cascadia字體。https://github.com/microsoft/cascadia-code/releases/download/v1911.21/CascadiaPL.ttf

oh-my-posh

oh-my-posh (https://github.com/JanDeDobbeleer/oh-my-posh) 收到oh-my-zsh的啟發(fā),實現(xiàn)了漂亮的prompt主題。

安裝:

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

其中,posh-git用于顯示git的狀態(tài)。

安裝之后,在powershell的profiles.ps1中加入:

Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox

oh-my-posh支持幾個主題(可以在github上找到具體介紹),這里使用個人認(rèn)為效果最好的Paradox主題。

安裝后的效果如下圖:

image

總結(jié)

本文通過編輯Powershell的profiles.ps1使用powershell函數(shù)實現(xiàn)了幾個常用的bash命令ls、whichtouch,同時介紹了利用oh-my-posh結(jié)合Windows Terminal的顏色字體個性化實現(xiàn)prompt主題自定義的方法。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容