Well this turned into a rabbit-trail-and-a-half ;-) ...
I would have thought this would have been pretty straightforward with the sendInput
action introduced in Windows Terminal 1.3, but I keep coming up short using that method. I don't claim to be any expert at keybindings or terminal escape sequences, but I'm trying to muddle through.
First, from man clear
, the "clear scrollback" sequence is Esc[3J
. This works in Windows Terminal via:
- Powershell:
Write-Output "`e[3J"
- bash, zsh, or fish:
printf '\033[3J'
(or equivalent echo
)
It, of course, needs to be coupled with the "clear entire screen" sequence (\033[2J
) and the "cursor to home position" (\033[H
) (obtained from this answer).
Put it together, and printf '\033[2J\033[3J\033[H'
gets us the clear
(without arguments) behavior.
So I would think, given the sendInput
example in the Windows Terminal 1.3 release notes that we could send that escape code via:
{ "command": { "action": "sendInput", "input": "\u001B[3J" }, "keys": "ctrl+shift+l"}
in the "actions"
section (formerly "keybindings"
). The \u001b[A
example (up arrow) works fine for me, but the "clear scrollback" results in an error bell in bash and printing [3J
in fish. The same occurs for \u001B[2J
for just trying to clear the screen as well. If someone can figure out where I'm going wrong on that, I'd love to know.
But ... we can rebind the keys in the shells. However, they probably won't won't be able to tell the difference between Ctrl+L and Ctrl+Shift+L for this reason. Except for the recent PSReadLine 2.0, which is case-sensitive on key-bindings.
So if you really want to use Ctrl+Shift+l in both PowerShell and Linux shells in WSL, you'll need to do it in two steps:
First, set up a Windows Terminal action to sendInput
with an unused keycode:
"actions":
// formerly "keybindings"
[
...
{ "command": { "action": "sendInput", "input": "\uAC12" }, "keys": "ctrl+shift+l"},
...
]
}
Honestly, I'm not sure what the best way of picking a keycode is. I just went with something from the Hangul character set (\uAC12
), since I'm fairly sure I'm never going to run across that in real-life. Note that this comment recommends using the "private use" E000-EFFF
range, but it seemed fairly populated on Windows Terminal, and I'd already settled on Hangul, so I've left the examples below using \uAC12
.
So then we need to bind \uAC12
in each shell to send the right escape sequences:
Bash:
First, get the key sequence to bind to with printf '\uAC12' | xxd
, which results in 00000000: eab0 92
.
Then, using that output, encode the bind command as bind -x '"\xEA\xB0\x92":"printf \\033[2J\\033[3J\\033[H"'
(xxd
idea from a comment in this answer.)
Zsh: It looks to be a bit more involved:
function clear-scrollback {
clear && printf '\e[3J'
zle && zle .reset-prompt && zle -R
}
zle -N clear-scrollback
bindkey '\uAC12' clear-scrollback
(with assist from this answer).
Fish: bind \uAC12 "printf '\033[2J\033[3J\033[H'; commandline -f repaint"
PowerShell: Set-PSReadLineKeyHandler "$([char]0xAC12)" -ScriptBlock { Write-Output "`e[2J`e[3J"; [Microsoft.PowerShell.PSConsoleReadLine]::ClearScreen() }
. Assist on the [char]
casting from this answer, and dang if it didn't take me a while to find an example of calling a PSReadline function in a ScriptBlock.
PowerShell Core: Set-PSReadLineKeyHandler "$([char]0xAC12)" -ScriptBlock { Write-Host "`e[2J`e[3J"; [Microsoft.PowerShell.PSConsoleReadLine]::ClearScreen() }
. Not sure why PowerShell and PowerShell Core behave differently here.
Corrections or suggestions for improvement welcome.
CLS
not do what you expect? I remember it asC
learL
aS
creen ;)cls
andclear
are aliases ofClear-Host
in PS. So what I really need in Windows Terminal is a way to bind theclear
command to a shortcut, which would work in both WSL and PS.alias
command.. for instance.. alias start='explorer.exe' .. then I can dostart .
just like I do from windows.clear
, Ctrl+y to cut the line, clear the screen, then yank (paste) back the result, but I'm lazy. Plus it's just easier to quickly hit a keyboard shortcut for this task - it's a muscle memory thing :)