Clear the terminal in VS Code
By Flavio Copes
Fix cmd-k not clearing the terminal in VS Code by editing the keybinding When clause to terminalFocus, or use the Terminal: Clear command from the palette.
To clear the terminal in VS Code you run the Terminal: Clear command: press cmd-shift-P to open the command palette, then search “Terminal: Clear”. On macOS it’s normally bound to cmd-k while the terminal is focused.
I usually clear the terminal with cmd-k, but on my machine this key combination didn’t work. Here’s how I fixed it.
Finding the broken keybinding
Open the keyboard shortcuts screen with cmd-k cmd-s (or search “Preferences: Open Keyboard Shortcuts” in the command palette) and look for workbench.action.terminal.clear. That’s the command behind Terminal: Clear.
Every keybinding has a When column. The shortcut only fires when that condition is true.
Mine was set to terminalFocus && terminalProcessSupported, and the second part was not evaluating to true, so pressing cmd-k did nothing even with the terminal focused.
The fix
I right-clicked the keybinding, chose Change When Expression, and set it to just terminalFocus. That removed the failing condition, and cmd-k cleared the terminal again.
You can also remove terminalFocus to make cmd-k clear the console even if the terminal is not focused, which is nice sometimes.
Be careful though: cmd-k is also the first key of many chained shortcuts in VS Code, like the cmd-k cmd-s we used above. If you bind it with no condition at all, those chains stop working, because VS Code runs the clear command before waiting for the second key.
Clearing vs typing clear
Typing clear in the shell is not the same thing. It scrolls the visible screen away, but VS Code keeps the scrollback: scroll up and the old output is still there.
The Terminal: Clear command wipes the scrollback buffer too. That’s the one I want when the output of previous commands starts confusing me: after running it, what I see is only what the next command prints.
Related posts about tools: