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

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


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)}


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