2

In particular I would like to send

ctrl + l

to clear the terminal so each time I test my script the terminal is clean.

user1873073
  • 3,580
  • 5
  • 46
  • 81
  • XY problem: if you want to clear the terminal, try asking about clearing the terminal, not about sending keystrokes :) http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Thomas May 25 '13 at 16:19
  • yes, but if there are two ways to do it and one is console.clear and the other is by sending keystrokes i will learn more from the second way which is why i phrased it like that. – user1873073 May 25 '13 at 16:20

1 Answers1

2

If you're on linux :

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

If you're on Windows, I think this works : (sets the cursor at 0,0)

var util = require('util');
util.print("\u001b[2J\u001b[0;0H");

We are basically using the power of ANSI escape codes:

These are ANSI escape codes. The first one (\033[2J) clears the entire screen (J) from top to bottom (2). The second code (\033[1;1H) positions the cursor at row 1, column 1.

https://stackoverflow.com/a/4062051/8932080

You may want to read the full Wikipedia article just here to fully understand it.

Puka
  • 1,485
  • 1
  • 14
  • 33
Ven
  • 19,015
  • 2
  • 41
  • 61
  • !! worked!. thanks. will mark as answer in a few minutes when it lets me. – user1873073 May 25 '13 at 16:19
  • For those of you who, like me, have no clues on what the above code is about, just read this answer which explains it very well: https://stackoverflow.com/a/4062051/8932080 – Puka Oct 03 '19 at 13:06