Skip to content

Add support for managing PSReadline settings from PSProfile #10

@scrthq

Description

@scrthq

Goal for this would be to enable storage of PSReadline Options, KeyHandlers, etc in your PSProfile configuration. These would ideally be ran as the appropriate command during PSProfile load, eliminating the need for a separate PSReadline settings script.

Initial approach: The commands below would be turned into the following hashtable property on $PSProfile.Settings:

Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Chord Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineKeyHandler -Key Alt+j -BriefDescription ShowDirectoryMarks -LongDescription "Show the currently marked directories" -ScriptBlock {
    param($key, $arg)
    $global:PSReadLineMarks.GetEnumerator() | ForEach-Object {
        [PSCustomObject]@{Key = $_.Key; Dir = $_.Value }
    } | Format-Table -AutoSize | Out-Host
    [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
}
$PSProfile.Settings.PSReadline = @{
    Options = @{
        HistorySearchCursorMovesToEnd = $true
    }
    KeyHandlers = @(
        @{
            Chord = 'Tab'
            Function = 'MenuComplete'
        }
        @{
            Key = 'UpArrow'
            Function = 'HistorySearchBackward'
        }
        @{
            Key = 'DownArrow'
            Function = 'HistorySearchForward'
        }
        @{
            Key = 'Alt+j'
            BriefDescription = 'ShowDirectoryMarks'
            LongDescription = 'Show the currently marked directories'
            ScriptBlock = 'param($key, $arg)
            $global:PSReadLineMarks.GetEnumerator() | ForEach-Object {
                [PSCustomObject]@{Key = $_.Key; Dir = $_.Value }
            } | Format-Table -AutoSize | Out-Host
            [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()'
        }
    )
}

During PSProfile import, the hashtable in $PSProfile.Settings.PSReadline would be parsed, converted to the original commands, then executed so they're available in the current session as they would normally be from an ExternalScript.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesthacktoberfestHacktoberfest issues to address

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions