F7 在 Powershell 7 中无法显示命令历史记录吗?

ger*_*imo 3 powershell powershell-7.0

在 Powershell 5.1 中,只要按一下F7,就会显示之前输入的命令的历史记录。我刚刚安装了 Powershell 7.2,但F7不再执行任何操作。F8尽管如此,仍然会根据命令历史记录自动完成。

Dav*_*ill 6

F7PowerShell 7 中不再执行任何操作。

\n

默认情况下它不执行任何操作。然而继续阅读...

\n

Get-PSReadLineKeyHandler列出了所有可用的键盘快捷键,并且从 PowerShell 7 开始F7不再包含在历史功能列表中:

\n
History functions\n=================\nKey       Function              Description\n---       --------              -----------\nAlt+F7    ClearHistory          Remove all items from the command line history (not PowerShell history)\nCtrl+s    ForwardSearchHistory  Search history forward interactively\nF8        HistorySearchBackward Search for the previous item in the history that starts with the current input - like PreviousHistory if the input is empty\nShift+F8  HistorySearchForward  Search for the next item in the history that starts with the current input - like NextHistory if the input is empty\nDownArrow NextHistory           Replace the input with the next item in the history\nUpArrow   PreviousHistory       Replace the input with the previous item in the history\nCtrl+r    ReverseSearchHistory  Search history backwards interactively\n
Run Code Online (Sandbox Code Playgroud)\n

about 输出来自Get-PSReadLineKeyHandler在 PowerShell 7.2.2 上运行

\n

奇怪的是,F7 Microsoft文档中仍然列出了有关 History - PowerShell | 的信息。Microsoft Docs所以它应该可以工作。

\n
\n

情节变得更加复杂

\n
\n

它仍然有效,只是不能与\nPSReadLine模块 (1)结合使用。我不确定这是否在任何地方都有记录,但\n您可以运行以下命令:

\n

Remove-Module -Name PSReadLine

\n

使用此 cmdlet 后,您将能够F7再次尽情享受,但您将错过所有精美的颜色;)

\n

请注意,Remove-Module实际上并没有删除模块,只是从当前会话中卸载它。

\n
\n

(1)强调我的

\n

Windows 10 AU 中不再有Source F7 历史记录?: 电源外壳

\n

有一个开放的错误报告请允许 F7 工作 \xc2\xb7 Issue #620 \xc2\xb7 PowerShell/PSReadLine

\n
\n

解决方法

\n

您可以使用以下脚本。

\n
\n

F7您可以使用Set-PSReadlineKeyHandler脚本中的 \n cmdlet在 PSReadline 中添加您自己的内容。例子:

\n
Set-PSReadlineKeyHandler -Key F7 -BriefDescription "History" -LongDescription "Show command history" -ScriptBlock {\n  $pattern = $null\n  [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $pattern, [ref] $null)\n  if ( $pattern ) {\n    $pattern = [Regex]::Escape($pattern)\n  }\n  $history = [System.Collections.ArrayList] @(\n    $last = ""\n    $lines = ""\n    foreach ( $line in [System.IO.File]::ReadLines((Get-PSReadlineOption).HistorySavePath) )\n      {\n      if ( $line.EndsWith(\'`\') ) {\n        $line = $line.Substring(0, $line.Length - 1)\n        $lines = if ( $lines ) { "$lines`n$line" } else { $line }\n        continue\n      }\n      if ( $lines ) {\n        $line = "$lines`n$line"\n        $lines = ""\n      }\n      if ( ($line -cne $last) -and ((-not $pattern) -or ($line -match $pattern)) ) {\n        $last = $line\n        $line\n      }\n    }\n  )\n  $command = $history | Out-GridView -Title History -PassThru\n  if ( $command ) {\n    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()\n    [Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n"))\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

当您使用此功能时,该F7键将出现在弹出的网格视图窗口中。选择历史记录条目并按\n Enter,PowerShell 会将其放在命令行上\n进行编辑。

\n
\n

源代码在Windows 10 Powershell中通过F7使命令历史记录弹出窗口工作 - 堆栈内存溢出,由Bill_Stewart回答

\n

另请参阅在 Powershell 中使用 F7 作为“显示命令历史记录”以获得类似的解决方法。

\n