PSReadLine icon indicating copy to clipboard operation
PSReadLine copied to clipboard

Allow accepting suggestion with the EndOfLine key (End) if cursor is at the end of line

Open Jackenmen opened this issue 2 years ago • 3 comments

Prerequisites

  • [X] Write a descriptive title.

Description of the new feature/enhancement

Currently, it is only possible to accept suggestions when at the end of line if the ForwardChar (right arrow) key is hit. I think it would also make sense to treat the EndOfLine (End) key the same way. I know that this is how zsh-autosuggestions work, I haven't tested if fish autosuggestions also work like that but I feel like it's an intuitive behavior since you're trying to go to the end of the suggested line. I'm sure it is possible with a script block (I haven't yet looked into how but probably will soon) but I think that this would be a sensible default to have.

Arguably, the End key makes more sense than the right arrow key, and the right arrow key could be used for accepting the next word but that's really just my opinion which doesn't really matter when there are already so many users that are used to this. + It is possible to change it with the provided ForwardCharAndAcceptNextSuggestionWord example.

Proposed technical implementation details (optional)

No response

[Edited by @daxian-dbw] Added one upvote to represent the duplicate issue https://github.com/PowerShell/PSReadLine/issues/3741

Jackenmen avatar Jun 03 '23 09:06 Jackenmen

Thanks for the issue @Jackenmen! You change change the AcceptSuggestion function to the End key to accept the suggestion. I do not know the exact key name of the End key but if you run [System.Console]::ReadKey(), press the end key and use that "Key" property to fill in this commend Set-PSReadLineKeyHandler -Chord '<END KEY PROPERTY>' -Function MenuComplete. I am restricted to a laptop currently so don't have that key to test myself 😄 Let me know if that works for you!

StevenBucher98 avatar Jun 04 '23 18:06 StevenBucher98

I was asking for a change in the default behavior of the End key such that:

  • it moves the cursor to the end of the current editing line, if it isn't already there
  • it accepts the suggestion, otherwise (i.e. when the cursor is at the end of the current editing line)

This would make it act more like the RightArrow key works:

  • it moves the cursor one character to the right, if it isn't at the end of the current editing line
  • it accepts the suggestion, otherwise (i.e. when the cursor is at the end of the current editing line)

Right now, I use this custom key handler to do this:

Set-PSReadLineKeyHandler -Key "End" `
        -BriefDescription ForwardCharAndAcceptNextSuggestionWord `
        -LongDescription "Move the cursor to the end of the current editing line or accept the suggestion if it's already there" `
        -ScriptBlock {
    param($key, $arg)

    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
    if ($cursor -lt $line.Length) {
        [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine($key, $arg)
    } else {
        [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion($key, $arg)
    }
}

but as I described in the issue, I think that this would be a sensible default to have which is why I'm requesting it. Personally, I feel like it's a weird inconsistency considering that both the right arrow and the End key are used for cursor movement.

Jackenmen avatar Jun 04 '23 19:06 Jackenmen

The relevant functions: https://github.com/PowerShell/PSReadLine/blob/9e7b1fe5249588a3814ae8f8e8ac3a96df48210c/PSReadLine/Movement.cs#L17-L34 https://github.com/PowerShell/PSReadLine/blob/9e7b1fe5249588a3814ae8f8e8ac3a96df48210c/PSReadLine/Movement.cs#L49-L66

Jackenmen avatar Jun 04 '23 19:06 Jackenmen