Skip to content

Commit c687848

Browse files
committed
[pwsh profile] Improve PSReadLine configuration.
1 parent e4c3740 commit c687848

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

PowerShell/Profile/Common/statements.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@ Set-PSReadLineOption -HistorySavePath (Join-Path (
1010
# Can be toggled with F2.
1111
Set-PSReadLineOption -PredictionViewStyle ListView
1212

13+
Set-PSReadLineKeyHandler -Chord Ctrl+Insert -Function Copy
14+
Remove-PSReadLineKeyHandler -Chord 'Ctrl+]' # Impossible chord on German keyboard
15+
Set-PSReadLineKeyHandler -Chord 'Ctrl+)' -Function GotoBrace
16+
17+
# Unified scrolling in stand-alone and VSCode terminal
18+
# See corresponding bindings in file:///./vscode-keybindings.jsonc
19+
Set-PSReadLineKeyHandler -Chord Ctrl+PageUp -Function ScrollDisplayUp
20+
Set-PSReadLineKeyHandler -Chord Ctrl+PageDown -Function ScrollDisplayDown
21+
Set-PSReadLineKeyHandler -Chord Ctrl+UpArrow -Function ScrollDisplayUpLine
22+
Set-PSReadLineKeyHandler -Chord Ctrl+DownArrow -Function ScrollDisplayDownLine
23+
Set-PSReadLineKeyHandler -Chord Ctrl+Home -Function ScrollDisplayTop
24+
Set-PSReadLineKeyHandler -Chord Ctrl+End -Function ScrollDisplayToCursor
25+
26+
if ($env:TERM_PROGRAM -eq 'vscode' <#-and $env:VSCODE_NONCE#>) {
27+
# $env:TERM_PROGRAM is defined for toplevel and sub shells.
28+
# $env:VSCODE_NONCE is only defined for toplevel shell. It gets removed by the shell integration
29+
# script, which is only run in the toplevel shell, *not* in sub shells.
30+
# We could mitigate by running that script ourselves in a sub shell, but don't really see
31+
# a use case yet.
32+
# See corresponding bindings in file:///./vscode-keybindings.jsonc
33+
Set-PSReadLineKeyHandler -Chord 'F12,m' -Function (Get-PSReadLineKeyHandler -Chord Ctrl+Enter).Function
34+
Set-PSReadLineKeyHandler -Chord 'F12,n' -Function (Get-PSReadLineKeyHandler -Chord Shift+Ctrl+Enter).Function
35+
Set-PSReadLineKeyHandler -Chord 'F12,o' -Function (Get-PSReadLineKeyHandler -Chord Ctrl+C).Function
36+
Set-PSReadLineKeyHandler -Chord 'F12,p' -Function (Get-PSReadLineKeyHandler -Chord 'Ctrl+)').Function
37+
Set-PSReadLineKeyHandler -Chord 'F12,q' -Function (Get-PSReadLineKeyHandler -Chord 'Alt+?').Function
38+
}
39+
1340
Set-PSReadLineKeyHandler -Chord Ctrl+F1 -BriefDescription 'Online Help' -Description 'Show online help for the command under or before the cursor.' -ScriptBlock {
1441
param($key, $arg)
1542
[System.Management.Automation.Language.Token[]]$tokens = $null
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// These VSCode keybindings are required for the PSReadLine configuration in file:///./statements.ps1
2+
[
3+
// PSReadline mitigations
4+
{ "key": "ctrl+enter", "command": "workbench.action.terminal.sendSequence",
5+
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
6+
"args": {"text":"\u001b[24~m"} },
7+
{ "key": "ctrl+shift+enter", "command": "workbench.action.terminal.sendSequence",
8+
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
9+
"args": {"text":"\u001b[24~n"} },
10+
{ "key": "ctrl+shift+c", "command": "workbench.action.terminal.sendSequence",
11+
"when": "terminalFocus && !terminalTextSelectedInFocused && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
12+
"args": {"text":"\u001b[24~o"} },
13+
{ "key": "ctrl+insert", "command": "workbench.action.terminal.sendSequence",
14+
"when": "terminalFocus && !terminalTextSelectedInFocused && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
15+
"args": {"text":"\u001b[24~o"} },
16+
{ "key": "ctrl+shift+9", "command": "workbench.action.terminal.sendSequence",
17+
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
18+
"args": { "text": "\u001b[24~p" } },
19+
{ "key": "shift+alt+oem_4", "command": "workbench.action.terminal.sendSequence",
20+
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
21+
"args": { "text": "\u001b[24~q" } },
22+
{ "key": "alt+0", "command": "-workbench.action.lastEditorInGroup" },
23+
{ "key": "alt+0", "command": "workbench.action.lastEditorInGroup",
24+
"when": "!terminalFocus" },
25+
26+
// Unified terminal scrolling
27+
{ "key": "ctrl+pageup", "command": "-workbench.action.terminal.focusPrevious",
28+
"when": "terminalFocus && terminalHasBeenCreated && !terminalEditorFocus || terminalFocus && terminalProcessSupported && !terminalEditorFocus" },
29+
{ "key": "ctrl+pageup", "command": "workbench.action.terminal.scrollUpPage",
30+
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
31+
{ "key": "ctrl+pagedown", "command": "-workbench.action.terminal.focusNext",
32+
"when": "terminalFocus && terminalHasBeenCreated && !terminalEditorFocus || terminalFocus && terminalProcessSupported && !terminalEditorFocus" },
33+
{ "key": "ctrl+pagedown", "command": "workbench.action.terminal.scrollDownPage",
34+
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
35+
{ "key": "ctrl+up", "command": "-workbench.action.terminal.scrollToPreviousCommand",
36+
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
37+
{ "key": "alt+up", "command": "workbench.action.terminal.scrollToPreviousCommand",
38+
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
39+
{ "key": "ctrl+down", "command": "-workbench.action.terminal.scrollToNextCommand",
40+
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
41+
{ "key": "alt+down", "command": "workbench.action.terminal.scrollToNextCommand",
42+
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
43+
{ "key": "ctrl+up", "command": "workbench.action.terminal.scrollUp",
44+
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
45+
{ "key": "ctrl+down", "command": "workbench.action.terminal.scrollDown",
46+
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
47+
]

0 commit comments

Comments
 (0)