How to Clear Console

clear javascript console in Google Chrome

Update: console.clear() is available in all browsers

Update: As of November 6, 2012, console.clear() is now available in Chrome Canary.


If you type clear() into the console it clears it.

I don't think there is a way to programmatically do it, as it could be misused. (console is cleared by some web page, end user can't access error information)

one possible workaround:

in the console type window.clear = clear, then you'll be able to use clear in any script on your page.

How to clear the interpreter console?

As you mentioned, you can do a system call:

For Windows:

>>> import os
>>> clear = lambda: os.system('cls')
>>> clear()

For Linux it would be:

>>> import os
>>> clear = lambda: os.system('clear')
>>> clear()

How to remove all recent console command

If you want to clear the list of last typed commands, follow these steps:

(Step 1 and 2 are important, don't skip them!)

  1. Undock the console (click on the icon in the bottom-left corner, undock icon).

    (if you don't see the undock icon, but Sample Image, then hold the mouse pressed for a few seconds to get the desired icon)
  2. Press Ctrl + Shift + J to open the console for this console. (On OSX use Cmd + Option + i)
  3. Go to the Resources tab, "Local Storage", chrome-devtools://devtools.
  4. Right-click on the item with key "consoleHistory", and choose "Delete".
    chrome-devtools://devtools -> consoleHistory -> Delete

  5. Done! You may close the new console, and then dock the previous one if wanted. The console history will be gone when you reload the console.

If you just want to clear the console log (not the commands), just press Ctrl + L.

You could also use Incognito mode if you don't want to keep the list of commands you're going to type.

Node.Js on windows - How to clear console

console.log('\033[2J');

This works on linux. Not sure about windows.

You can "trick" the user using something like this:

var lines = process.stdout.getWindowSize()[1];
for(var i = 0; i < lines; i++) {
console.log('\r\n');
}

Clear terminal in Python

What about escape sequences?

print(chr(27) + "[2J")

Chrome console clear assignment and variables

Easiest way to clear data from the console is to refresh the page.

What you are affecting when declaring any variables or functions within the developer console is the global execution context, which for web browsers is window.

When you clear() the console you are telling Chrome to remove all visible history of these operations, not clear the objects that you have attached to window.

How can I clear the terminal in Visual Studio Code?

Use Ctrl+K. This goes clean your console in Visual Studio Code.

Per comments, in later versions of VSCode (1.29 and above) this shortcut is missing / needs to be created manually.

  • Navigate: File > Preferences > Keyboard Shortcuts
  • search for workbench.action.terminal.clear
  • If it has no mapping or you wish to change the mapping, continue; otherwise note & use the existing mapping
  • Double click on this entry & you'll be prompted for a key binding. Hold CTRL and tap K. Ctrl + K should now be listed. Press enter to save this mapping
  • Right click the entry and select Change when expression. Type terminalFocus then press enter.
  • That's it. Now, when the terminal is in focus and you press Ctrl+K you'll get the behaviour you'd have expected to get from running clear/cls.


Related Topics



Leave a reply



Submit