关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
3年前关闭。
Improve this question
在 Powershell 控制台中,在 Windows 10 (10.0.17134.0) 上,F7 不会像在 CMD 控制台中那样弹出命令历史记录。我们如何在 PS 控制台窗口中完成这项工作?关于 $Profile
的任何提示这样做的语法?
最佳答案
如您所见,这是因为 PSReadline 模块默认安装在 Windows 10 上。您可以使用 Set-PSReadlineKeyHandler
在 PSReadline 中添加自己的 F7。脚本中的 cmdlet。例子:
Set-PSReadlineKeyHandler -Key F7 -BriefDescription "History" -LongDescription "Show command history" -ScriptBlock {
$pattern = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $pattern, [ref] $null)
if ( $pattern ) {
$pattern = [Regex]::Escape($pattern)
}
$history = [System.Collections.ArrayList] @(
$last = ""
$lines = ""
foreach ( $line in [System.IO.File]::ReadLines((Get-PSReadlineOption).HistorySavePath) ) {
if ( $line.EndsWith('`') ) {
$line = $line.Substring(0, $line.Length - 1)
$lines = if ( $lines ) { "$lines`n$line" } else { $line }
continue
}
if ( $lines ) {
$line = "$lines`n$line"
$lines = ""
}
if ( ($line -cne $last) -and ((-not $pattern) -or ($line -match $pattern)) ) {
$last = $line
$line
}
}
)
$command = $history | Out-GridView -Title History -PassThru
if ( $command ) {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n"))
}
}
使用此功能时,F7 键将出现在弹出的 GridView 窗口中。选择一个历史条目并按 Enter,PowerShell 会将其放在命令行上进行编辑。
关于powershell - 在 Windows 10 Powershell 中通过 F7 使命令历史弹出窗口工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50376858/