Appearance
notepad $PROFILE
ps1
# 启用列表视图(更像 Zsh,更直观)
Set-PSReadLineOption -PredictionViewStyle ListView
# 启用历史命令预测(输入前缀自动显示用过的命令)
Set-PSReadLineOption -PredictionSource History
# 绑定 Delete 键:删除高亮的命令(英文提示,无编码问题)
Set-PSReadLineKeyHandler -Chord 'Delete' -ScriptBlock {
# 获取当前命令行
$currentLine = ''
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $currentLine, [ref] $null)
# 如果命令行是空的,执行普通删除字符功能
if ([string]::IsNullOrWhiteSpace($currentLine)) {
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteChar()
return
}
# 获取历史文件路径并清空内存中的历史
$historyPath = (Get-PSReadLineOption).HistorySavePath
[Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory()
# 读取所有历史命令,准备重写文件
$allHistory = [System.IO.File]::ReadAllLines($historyPath)
Clear-Content $historyPath
# 跳过与当前命令行完全匹配的项
$lineRemoved = $false
foreach ($command in $allHistory) {
if ($command -eq $currentLine) {
$lineRemoved = $true
continue
}
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($command)
}
# 清除屏幕上的命令行
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteLine()
# 显示提示(使用英文,避免编码问题)
if ($lineRemoved) {
$host.UI.Write("Command '$currentLine' removed from history.")
}
else {
$host.UI.Write("Command '$currentLine' not found in history.")
}
Start-Sleep -Milliseconds 1500
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
}