OS

Fix VSCode terminal: fully clear scrollback with cls/clear

Resolve VSCode's cls/clear not removing scrollback. Step‑by‑step fixes for PowerShell, Bash and an extension to fully clear the integrated terminal history.

1 answer 1 view

How can I fix the Visual Studio Code integrated terminal so that the cls or clear command completely clears the terminal history instead of just clearing the visible output? When I run these commands, only the visible part gets cleared, but I can still see previous commands when scrolling up. I’m looking for a solution that doesn’t involve using keyboard shortcuts or alternative methods.

The Visual Studio Code integrated terminal’s cls or clear command only clears the visible output, leaving the scrollback buffer intact. You can fix this by configuring your shell profile to properly clear both the viewport and scrollback buffer, or by installing an extension that intercepts these commands. This issue affects PowerShell, Bash, and other shells differently, requiring shell-specific solutions to fully erase the terminal history.

Contents

Problem Overview: cls/clear only clears viewport, not scrollback

When you run cls in PowerShell or clear in Bash within the Visual Studio Code integrated terminal, it only clears the visible screen content. However, when you scroll up, you can still see all previous commands and output. This happens because VS Code’s terminal implementation doesn’t fully process the standard escape sequences that normally clear the scrollback buffer in traditional terminal emulators.

According to GitHub issue discussions, the clear command works by outputting escape sequences to the screen. Since the integrated terminal reports its terminal type as TERM = xterm-256color, it should respond to the escape sequence printf '\e[3J' which clears the scrollback buffer on most xterm-compatible terminals. However, in VS Code, this sequence doesn’t work as expected.

Why this happens: xterm.js behavior and TERM escape sequences

The Visual Studio Code integrated terminal is built on xterm.js, a JavaScript terminal emulator library. This implementation handles escape sequences differently from native terminal emulators. When you use cls or clear, they typically output a specific sequence of escape codes: one to clear the visible screen (\e[2J) and another to clear the scrollback buffer (\e[3J).

The official VS Code documentation explains that the terminal content is called the buffer, with the section above the viewport being called “scrollback”. The amount of scrollback kept is controlled by the terminal.integrated.scrollback setting, which defaults to 1000 lines. This explains why your terminal history persists even after running clear.

As noted in Stack Overflow discussions, the built-in “Terminal: Clear” command (workbench.action.terminal.clear) does clear both the screen and the scrollback buffer, which is why it works as expected when executed via the Command Palette or keyboard shortcut.

PowerShell (Windows) Solution

For PowerShell users on Windows, there are two effective solutions that make cls work properly in the VS Code integrated terminal:

Method 1: Enable PowerShell Force Clear Scrollback Setting

  1. Open VS Code settings (Ctrl+,)
  2. Search for “PowerShell > Integrated Console: Force Clear Scrollback Buffer”
  3. Check this option to enable it

This setting forces PowerShell’s Clear-Host (the cmdlet behind cls) to erase the integrated terminal’s scrollback buffer, not just the viewport. As explained in Stack Overflow, this is the most straightforward solution for PowerShell users.

Method 2: Create a cls Alias in Your PowerShell Profile

If you prefer not to modify settings or want a more robust solution, you can create a custom cls alias in your PowerShell profile that clears both the viewport and scrollback buffer:

powershell
# Check if your profile exists
Test-Path $PROFILE

# If not, create it
if (!(Test-Path $PROFILE)) { 
    New-Item -Path $PROFILE -ItemType File -Force 
}

# Add this function to your profile
function global:cls { 
    Clear-Host 
    $host.UI.RawUI.ClearScrollBuffer()
}

# Save the file and restart VS Code or reload the profile with . $PROFILE

This solution, documented in GitHub issue #45137, ensures that every time you type cls, it will clear both the visible screen and the scrollback buffer.

Bash / Zsh / Other Shells Solution

For Bash, Zsh, or other Unix-like shells, you need to modify your shell profile to include an alias that properly clears the scrollback buffer:

Method 1: Add an Alias to Your Shell Profile

Add the following line to your shell profile file (~/.bashrc, ~/.zshrc, etc.):

bash
alias clear='clear && printf "\e[3J"'

This alias first runs the normal clear command to clear the visible screen, then prints the escape sequence \e[3J which should clear the scrollback buffer. As mentioned in Superuser discussions, this approach works on most terminals but may have inconsistent results in VS Code’s integrated terminal.

Method 2: Create a Clear Function

For more control, you can create a function instead of an alias:

bash
clear() {
    builtin clear
    printf '\e[3J'
}

Add this to your shell profile and restart your terminal or source the profile file.

Note for VS Code on Linux/macOS

If you’re using VS Code on Linux or macOS, you might also consider using the keyboard shortcut Cmd+K (macOS) or Ctrl+K (Linux) which, according to the official VS Code documentation, executes the “Terminal: Clear” command that properly clears both viewport and scrollback.

Extension Solution: True Clear Terminal

If you prefer a non-code solution that works across all shells without modifying profiles, you can install the “True Clear Terminal” extension from the VS Code Marketplace.

  1. Open the Extensions view in VS Code (Ctrl+Shift+X or Cmd+Shift+X)
  2. Search for “True Clear Terminal”
  3. Click “Install” on the extension by mohammadalshikh

This extension, available on the Visual Studio Marketplace, works by intercepting cls and clear commands in any shell and executing VS Code’s internal clear command (workbench.action.terminal.clear) instead. This ensures that both the viewport and scrollback buffer are cleared regardless of which shell you’re using.

The extension provides a global override that requires no keyboard shortcuts and works cross-platform. It’s particularly useful if you frequently switch between different shells and want consistent clearing behavior.

Testing & Troubleshooting

After implementing any of these solutions, you should test whether the scrollback buffer is properly cleared:

  1. Run several commands that produce output
  2. Type cls (PowerShell) or clear (Bash/Zsh)
  3. Scroll up to check if previous commands are still visible

If you still see previous commands after clearing:

  1. For PowerShell solutions: Verify that the setting is enabled or the profile was loaded correctly. You can check by running Get-ExecutionPolicy to ensure your profile can run scripts.

  2. For Bash/Zsh solutions: Ensure your profile was sourced correctly by running source ~/.bashrc or source ~/.zshrc.

  3. For the extension: Try uninstalling and reinstalling the extension if it doesn’t work as expected.

  4. Persistent issues: Some VS Code terminal sessions might retain scrollback between restarts. Try completely closing and reopening the terminal pane.

Remember that different shells may behave differently in the VS Code integrated terminal. What works perfectly in PowerShell might need adjustment for Bash, and vice versa. The extension option provides the most consistent behavior across all shells.

Sources

Conclusion

Fixing the Visual Studio Code integrated terminal so that cls or clear completely clears the terminal history requires understanding that VS Code’s terminal implementation doesn’t fully process standard escape sequences. For PowerShell users, enabling the “Force Clear Scrollback Buffer” setting or creating a custom cls function that calls $host.UI.RawUI.ClearScrollBuffer() provides an effective solution. For Bash/Zsh users, adding an alias that prints the \e[3J escape sequence after clearing can work, though results may vary. The most consistent approach across all shells is installing the “True Clear Terminal” extension, which intercepts these commands and executes VS Code’s internal clear command. By implementing one of these solutions, you’ll ensure that cls and clear truly erase all terminal history, not just the visible output.

Authors
Verified by moderation
Moderation
Fix VSCode terminal: fully clear scrollback with cls/clear