How to Clear the 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()

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');
}

How to clear the console screen when introducing input in Java?

You cannot use System.in without pressing the ENTER key at the end. Its the only way. System.in is not aware of the user input until enter is pressed. So if you are using a normal command line to do this, this won't work and you have to stick different raw command lines like jline

For example :

Terminal terminal = TerminalBuilder.builder().system(true).jna(true).build();
terminal.enterRawMode(); //this enters into a raw mode and get's input on reader
reader = terminal.reader();
//finally
reader.close();

You can check it out. Although it highly doesn't make sense to bring in more dependencies unless you really want to and could just stick to a simple while loop

If you are using maven here's the dependency you can use.

<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
</dependency>

Function to clear the console in R and RStudio

cat("\014")  

is the code to send CTRL+L to the console, and therefore will clear the screen.

Far better than just sending a whole lot of returns.

How to clear the screen of Interactive IPython console from Python code

(Spyder maintainer here) There are two ways to get what you want:

  1. You can go to the menu Run > Configuration per file and select the option called Execute in a dedicated console. That option will clean your console after every execution.
  2. Add the following code to your file:

    from IPython import get_ipython
    get_ipython.run_line_magic('clear', '')

how to clear console in Java on Windows

The link in answer by @Olivier does work but unfortunately most suggestions use a sub-process cls or don't explain how to enable on Windows.

In latest Windows 10 you can use ANSI code support in Window Terminal, but not directly in CMD.EXE consoles. To enable for CMD.EXE add the registry key VirtualTerminalLevel mentioned in these answers for ANSI colours and then you can print the appropriate ANSI/VT codes directly without running a sub-process:

System.out.print("\033[2J\033[1;1H");

Or

System.out.print(ANSI.CLEAR_SCREEN+ANSI.position(1, 1));

where simple definition of ANSI codes is:

public class ANSI {
// Control Sequence Introducer:
public static String CSI = "\u001b[";

public static String CLEAR_SCREEN = CSI+"2J";
public static String position(int row, int col) {
return CSI+row+";"+col+"H";
}
}

Note that other terminals or such as those in IDEs may not support the above.



Related Topics



Leave a reply



Submit