3

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

Set-PSReadlineOption -EditMode vi

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

svn help x-shelve --list

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

svn help x-shelve --list|

而不是

|svn help x-shelve --list

有没有办法设置这个?

4

2 回答 2

2

您可以使用Set-PSReadLineKeyHandlercmdlet:

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)
}
于 2018-10-23T07:53:15.957 回答
2

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

Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$true

您可以查看可以设置哪些选项Get-PSReadLineOption

Get-PSReadLineOption

在线文档包括一些有用的示例

于 2018-12-20T05:11:53.150 回答