4

I'm using the powershell VI mode via

Set-PSReadlineOption -EditMode vi

It is awsome to be able to edit the line using VI commands however there is one thing that is annoying. When using the up and down arrows to navigate history the cursor always starts at the start of the line instead of at the end. ie: if I had the following command in my history

svn help x-shelve --list

then I would want the cursor (represented by the pipe | ) to be like

svn help x-shelve --list|

rather than

|svn help x-shelve --list

is there a way to set this?

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217

2 Answers2

4

Use the same Set-PSReadLineOption cmdlet that you used to get into VI mode:

Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$true

You can see which options can be set with Get-PSReadLineOption:

Get-PSReadLineOption

and the online documentation includes a few useful examples

Darren G
  • 151
  • 1
  • 3
2

You can use the 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)
}
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
Peter Schneider
  • 2,879
  • 1
  • 14
  • 17
  • 1
    Perhaps the option was added in a later version or something, but the below answer is much simpler and more "correct" – Orangutech Jun 21 '19 at 15:40