Intro
提到shell岁诉,似乎大家首先想到Linux,其實(shí)Windows的Powershell也是非常好的命令行界面空入。同時(shí)由于Powershell默認(rèn)狀態(tài)下已經(jīng)把很多常用的基礎(chǔ)bash命令(如cd
和ls
)都設(shè)置成了別名锭亏,因而對(duì)于一些常用操作基本上可以當(dāng)成bash來用。當(dāng)然還有一些缺失十绑,比如which
和touch
等命令聚至,則需要通過自定義函數(shù)利用Powershell命令實(shí)現(xiàn)。另一方面本橙,Powershell的藍(lán)色背景白色字符實(shí)在是缺乏美感扳躬,也影響日常使用,因而需要選擇一個(gè)終端并進(jìn)行美化工作甚亭。因而本文首先介紹幾個(gè)自定義的常用函數(shù)的方法贷币,同時(shí)進(jìn)一步介紹使用oh-my-posh和Windows Terminal構(gòu)建美觀的Powershell終端。
常用bash命令實(shí)現(xiàn)
由于powershell自帶的別名有缺失或默認(rèn)功能與bash命令有所不同亏狰,因而需要自定義和重載一些函數(shù)役纹。這些函數(shù)需要在profile.ps1
中定義,這樣每次打開powershell時(shí)即可自動(dòng)加載暇唾。以下介紹3個(gè)常用命令促脉。
ls
ls
時(shí)Powershell自帶的別名啰挪,然而其輸出的結(jié)果默認(rèn)為完整信息形式且沒有用不同顏色表示文件夾或文件。
這里使用Get-ChildItemColor
實(shí)現(xiàn)不同文件類型以不同顏色顯示嘲叔,同時(shí)增加不同別名來實(shí)現(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)而實(shí)現(xiàn)使用ls
列出文件名,使用l
列出詳細(xì)列表硫戈。
touch
Powershell默認(rèn)沒有定義touch
命令的別名锰什,這里使用以下函數(shù)使用New-Item
實(shí)現(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
實(shí)現(xiàn)which
汁胆。
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
~
Powershell沒有使用~
來代表用戶目錄,可以通過簡(jiǎn)單的Set-Alias
來實(shí)現(xiàn)霜幼。
function cuserprofile { Set-Location ~ }
Set-Alias ~ cuserprofile -Option AllScope
終端美化
終端美化有兩個(gè)目標(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:
點(diǎn)擊Windows Terminal的Settings會(huì)直接打開profiles.json
文件驹针。在其中可以進(jìn)行各種個(gè)性化設(shè)置烘挫。以下是修改后的效果以及其對(duì)應(yīng)的json配置文件。
// 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中的一個(gè)主題,更多適用于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ā)撤防,實(shí)現(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支持幾個(gè)主題(可以在github上找到具體介紹),這里使用個(gè)人認(rèn)為效果最好的Paradox主題。
安裝后的效果如下圖:
總結(jié)
本文通過編輯Powershell的profiles.ps1
使用powershell函數(shù)實(shí)現(xiàn)了幾個(gè)常用的bash命令ls
漾肮、which
和touch
厂抖,同時(shí)介紹了利用oh-my-posh結(jié)合Windows Terminal的顏色字體個(gè)性化實(shí)現(xiàn)prompt主題自定義的方法。