user1873073
user1873073

Reputation: 3670

Is there anyway to send keystrokes to the terminal in node?

In particular I would like to send

ctrl + l

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

Upvotes: 2

Views: 741

Answers (1)

Ven
Ven

Reputation: 19040

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.

Upvotes: 2

Related Questions