Home > Back-end >  Powershell 7: Bind my function to shortcut
Powershell 7: Bind my function to shortcut

Time:10-04

I am trying to bind something like "go to parent dir" to a key command (ctrl alt u).

I am new to powershell and still trying to get used to it [but let us be honest, I am a newbie]. I got the command Split-Path $pwd | Push-Location which basically does what I want: Go to the parent directory of the current path.

Now I try to bind that command to Ctrl Alt u but am desperately lost....

I copy-pasted the following which does not work [PSReadLine module is installed, but maybe not the right tool for it??]:

Set-PSReadlineKeyHandler -Key Ctrl Alt u `
                         -BriefDescription ParentDirectory `
                         -LongDescription "Push parent directory" `
                         -ScriptBlock {
    Split-Path $pwd | Push-Location
}

Any help is appreciated. I tried to find useful documentation on this topic to solve it on my own but I was not successful.

CodePudding user response:

Ctrl Alt u as defined above works for me however does not show new prompt immediately (I need to press Enter manually)…

  • Windows 10
  • PowerShell 7.2.6
  • module PSReadLine 2.1.0)

The following improved user-defined key binding should do the trick:

Set-PSReadlineKeyHandler -Chord Ctrl Alt u `
                         -BriefDescription ParentDirectory `
                         -LongDescription "Push parent directory" `
                         -ScriptBlock {
    Split-Path $pwd | Push-Location
    [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
  • Related