在带有 psreadline -EditMode VI 的 powershell 中,如何确保光标在浏览历史记录时从行尾开始

In powershell with psreadline -EditMode VI how to ensure the cursor starts at the end of the line when going through history

我正在通过

使用 powershell VI 模式
Set-PSReadlineOption -EditMode vi

能够使用 VI 命令编辑行真是太棒了,但是有一件事很烦人。当使用向上和向下箭头浏览历史时,光标总是从行的开头而不是行尾开始。即:如果我的历史记录中有以下命令

svn help x-shelve --list

那么我希望光标 (由管道 | 表示)

svn help x-shelve --list|

而不是

|svn help x-shelve --list

有办法设置吗?

您可以使用 Set-PSReadLineKeyHandler cmdlet:

Set-PSReadLineKeyHandler -Key UpArrow `
   -ScriptBlock {
     param($key, $arg)

     $line=$null
     $cursor=$null
     [Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchBackward()
     [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
     [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($line.Length)
}


Set-PSReadLineKeyHandler -Key DownArrow `
   -ScriptBlock {
     param($key, $arg)

     $line=$null
     $cursor=$null
     [Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchForward()
     [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
     [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($line.Length)
}

使用与进入 VI 模式相同的 Set-PSReadLineOption cmdlet:

Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$true

你可以看到哪些选项可以用Get-PSReadLineOption设置:

Get-PSReadLineOption

并且 online documentation 包含一些有用的示例