Skip to content

Commit

Permalink
[pwsh profile] Improve PSReadLine configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mawosoft committed May 18, 2024
1 parent e4c3740 commit c687848
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
27 changes: 27 additions & 0 deletions PowerShell/Profile/Common/statements.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ Set-PSReadLineOption -HistorySavePath (Join-Path (
# Can be toggled with F2.
Set-PSReadLineOption -PredictionViewStyle ListView

Set-PSReadLineKeyHandler -Chord Ctrl+Insert -Function Copy
Remove-PSReadLineKeyHandler -Chord 'Ctrl+]' # Impossible chord on German keyboard
Set-PSReadLineKeyHandler -Chord 'Ctrl+)' -Function GotoBrace

# Unified scrolling in stand-alone and VSCode terminal
# See corresponding bindings in file:///./vscode-keybindings.jsonc
Set-PSReadLineKeyHandler -Chord Ctrl+PageUp -Function ScrollDisplayUp
Set-PSReadLineKeyHandler -Chord Ctrl+PageDown -Function ScrollDisplayDown
Set-PSReadLineKeyHandler -Chord Ctrl+UpArrow -Function ScrollDisplayUpLine
Set-PSReadLineKeyHandler -Chord Ctrl+DownArrow -Function ScrollDisplayDownLine
Set-PSReadLineKeyHandler -Chord Ctrl+Home -Function ScrollDisplayTop
Set-PSReadLineKeyHandler -Chord Ctrl+End -Function ScrollDisplayToCursor

if ($env:TERM_PROGRAM -eq 'vscode' <#-and $env:VSCODE_NONCE#>) {
# $env:TERM_PROGRAM is defined for toplevel and sub shells.
# $env:VSCODE_NONCE is only defined for toplevel shell. It gets removed by the shell integration
# script, which is only run in the toplevel shell, *not* in sub shells.
# We could mitigate by running that script ourselves in a sub shell, but don't really see
# a use case yet.
# See corresponding bindings in file:///./vscode-keybindings.jsonc
Set-PSReadLineKeyHandler -Chord 'F12,m' -Function (Get-PSReadLineKeyHandler -Chord Ctrl+Enter).Function
Set-PSReadLineKeyHandler -Chord 'F12,n' -Function (Get-PSReadLineKeyHandler -Chord Shift+Ctrl+Enter).Function
Set-PSReadLineKeyHandler -Chord 'F12,o' -Function (Get-PSReadLineKeyHandler -Chord Ctrl+C).Function
Set-PSReadLineKeyHandler -Chord 'F12,p' -Function (Get-PSReadLineKeyHandler -Chord 'Ctrl+)').Function
Set-PSReadLineKeyHandler -Chord 'F12,q' -Function (Get-PSReadLineKeyHandler -Chord 'Alt+?').Function
}

Set-PSReadLineKeyHandler -Chord Ctrl+F1 -BriefDescription 'Online Help' -Description 'Show online help for the command under or before the cursor.' -ScriptBlock {
param($key, $arg)
[System.Management.Automation.Language.Token[]]$tokens = $null
Expand Down
47 changes: 47 additions & 0 deletions PowerShell/Profile/Common/vscode-keybindings.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// These VSCode keybindings are required for the PSReadLine configuration in file:///./statements.ps1
[
// PSReadline mitigations
{ "key": "ctrl+enter", "command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
"args": {"text":"\u001b[24~m"} },
{ "key": "ctrl+shift+enter", "command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
"args": {"text":"\u001b[24~n"} },
{ "key": "ctrl+shift+c", "command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus && !terminalTextSelectedInFocused && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
"args": {"text":"\u001b[24~o"} },
{ "key": "ctrl+insert", "command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus && !terminalTextSelectedInFocused && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
"args": {"text":"\u001b[24~o"} },
{ "key": "ctrl+shift+9", "command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
"args": { "text": "\u001b[24~p" } },
{ "key": "shift+alt+oem_4", "command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus && terminalShellIntegrationEnabled && !accessibilityModeEnabled && terminalShellType == 'pwsh'",
"args": { "text": "\u001b[24~q" } },
{ "key": "alt+0", "command": "-workbench.action.lastEditorInGroup" },
{ "key": "alt+0", "command": "workbench.action.lastEditorInGroup",
"when": "!terminalFocus" },

// Unified terminal scrolling
{ "key": "ctrl+pageup", "command": "-workbench.action.terminal.focusPrevious",
"when": "terminalFocus && terminalHasBeenCreated && !terminalEditorFocus || terminalFocus && terminalProcessSupported && !terminalEditorFocus" },
{ "key": "ctrl+pageup", "command": "workbench.action.terminal.scrollUpPage",
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
{ "key": "ctrl+pagedown", "command": "-workbench.action.terminal.focusNext",
"when": "terminalFocus && terminalHasBeenCreated && !terminalEditorFocus || terminalFocus && terminalProcessSupported && !terminalEditorFocus" },
{ "key": "ctrl+pagedown", "command": "workbench.action.terminal.scrollDownPage",
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
{ "key": "ctrl+up", "command": "-workbench.action.terminal.scrollToPreviousCommand",
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
{ "key": "alt+up", "command": "workbench.action.terminal.scrollToPreviousCommand",
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
{ "key": "ctrl+down", "command": "-workbench.action.terminal.scrollToNextCommand",
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
{ "key": "alt+down", "command": "workbench.action.terminal.scrollToNextCommand",
"when": "terminalFocus && terminalHasBeenCreated && !accessibilityModeEnabled || terminalFocus && terminalProcessSupported && !accessibilityModeEnabled" },
{ "key": "ctrl+up", "command": "workbench.action.terminal.scrollUp",
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
{ "key": "ctrl+down", "command": "workbench.action.terminal.scrollDown",
"when": "terminalFocusInAny && terminalHasBeenCreated && !terminalAltBufferActive || terminalFocusInAny && terminalProcessSupported && !terminalAltBufferActive" },
]

0 comments on commit c687848

Please sign in to comment.