Skip to content

Commit 5ba0780

Browse files
committed
[pwsh profile] Add PSReadLine key handler
1 parent 0049bc6 commit 5ba0780

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

PowerShell/Profile/Common/statements.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,23 @@ Set-PSReadLineOption -HistorySavePath (Join-Path (
99
) 'PowerShell\PSReadLine\AllHosts_history.txt' )
1010
# Can be toggled with F2.
1111
Set-PSReadLineOption -PredictionViewStyle ListView
12+
13+
Set-PSReadLineKeyHandler -Chord Ctrl+F1 -BriefDescription 'Online Help' -Description 'Show online help for the command under or before the cursor.' -ScriptBlock {
14+
param($key, $arg)
15+
[System.Management.Automation.Language.Token[]]$tokens = $null
16+
[int]$cursor = 0
17+
[string]$commandName = ''
18+
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$null, [ref]$tokens, [ref]$null, [ref]$cursor)
19+
$null = $null
20+
#Write-Warning 'debug' -WarningAction Break
21+
foreach ($token in $tokens) {
22+
if ($token.Extent.StartOffset -gt $cursor) { break }
23+
if ($token.TokenFlags -band [System.Management.Automation.Language.TokenFlags]::CommandName) {
24+
$commandName = $token.Text
25+
}
26+
}
27+
if ($commandName) {
28+
# -ErrorAction Ignore has no effect
29+
try { $null = Get-Help $commandName -Online } catch {}
30+
}
31+
}

0 commit comments

Comments
 (0)