以下を$PROFILEに記述すると、Kaliっぽくなる。
PowerShell 7向け。
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineKeyHandler -Key Ctrl+e -Function AcceptSuggestion
Set-PSReadLineKeyHandler -Key RightArrow -Function ForwardChar
# Tab を Linux みたいに「完了」動作にする
# Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Chord Tab -Function MenuComplete
# Set-PSReadLineOption -PredictionViewStyle ListView
# Set-PSReadLineOption -PredictionViewStyle InlineView
# 候補数を増やす(古い版でも有効)
Set-PSReadLineOption -CompletionQueryItems 500
Set-PSReadLineOption -Colors @{
Command = 'Cyan'
Error = 'DarkGray'
}
# vi
Set-Alias vi vim
function prompt {
# path shortener
$full = $PWD.ProviderPath
$userHome = [Environment]::GetFolderPath("UserProfile")
if ($full -like "$userHome*") {
$shortPath = "~" + $full.Substring($userHome.Length)
}
else {
$shortPath = $full
}
# ===== Git ブランチ(リポジトリルート探索版) =====
function Find-GitRoot($startDir) {
$dir = Get-Item $startDir
while ($dir -ne $null) {
if (Test-Path (Join-Path $dir.FullName ".git")) {
return $dir.FullName
}
$dir = $dir.Parent
}
return $null
}
$branch = ""
try {
$gitRoot = Find-GitRoot $PWD
if ($gitRoot) {
$gitHead = Join-Path $gitRoot ".git\HEAD"
if (Test-Path $gitHead) {
$content = Get-Content $gitHead -ErrorAction SilentlyContinue
if ($content -match "ref: refs/heads/(.+)") {
$branch = $matches[1]
}
}
}
}
catch {}
# User / Host
$user = [Environment]::UserName
$hostName = $env:COMPUTERNAME
# 1行目
Write-Host "┌──(" -ForegroundColor Green -NoNewline
Write-Host $user -ForegroundColor Blue -NoNewline
Write-Host "㉿" -ForegroundColor Blue -NoNewline
Write-Host $hostName -ForegroundColor Blue -NoNewline
Write-Host ")-[" -ForegroundColor Green -NoNewline
Write-Host $shortPath -ForegroundColor White -NoNewline
Write-Host "]" -ForegroundColor Green -NoNewline
if ($branch) {
Write-Host "($branch)" -ForegroundColor Blue -NoNewline
}
Write-Host ""
# 2行目
Write-Host "└─$ " -ForegroundColor Green -NoNewline
return " "
}