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

时间:2018-10-23 05:36:05

标签: powershell psreadline

我正在通过以下方式使用Powershell VI模式

Set-PSReadlineOption -EditMode vi

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

svn help x-shelve --list

然后我希望光标(由管道|表示)

svn help x-shelve --list|

而不是

|svn help x-shelve --list

有没有办法设置这个?

2 个答案:

答案 0 :(得分:2)

您可以使用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)
}

答案 1 :(得分:0)

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

Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$true

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

Get-PSReadLineOption

online documentation包含一些有用的示例