Question: I have a simple readline shell written in Coffeescript:

```coffee
rl = require 'readline'
cli = rl.createInterface process.stdin, process.stdout, null
cli.setPrompt "hello> "

cli.on 'line', (line) ->
  console.log line
  cli.prompt()

cli.prompt()

```

Running this displays a prompt:

```
$ coffee cli.coffee 
hello> 

```

I would like to be able to hit `Ctrl-L` to clear the screen. Is this possible?

I have also noticed that I cannot hit `Ctrl-L` in either the **node** or **coffee** REPLs either.

I am running on Ubuntu 11.04.

Answer: 
You can watch for the keypress yourself and clear the screen.

```
process.stdin.on 'keypress', (s, key) ->
  if key.ctrl && key.name == 'l'
    process.stdout.write '\u001B[2J\u001B[0;0f'

```

Clearing is done with ASCII control sequences like those written here:
<http://ascii-table.com/ansi-escape-sequences-vt-100.php>

The first code `\u001B[2J` instructs the terminal to clear itself, and the second one `\u001B[0;0f` forces the cursor back to position 0,0.

### Note

The `keypress` event is no longer part of the standard Node API in Node `>= 0.10.x` but you can use the [keypress](https://github.com/TooTallNate/keypress) module instead.
This is the only answer that will clear the screen **AND** scroll history.

```
function clear() {
  // 1. Print empty lines until the screen is blank.
  process.stdout.write('\033[2J');

  // 2. Clear the scrollback.
  process.stdout.write('\u001b[H\u001b[2J\u001b[3J');
}

// Try this example to see it in action!
(function loop() {
  let i = -40; // Print 40 lines extra.
  (function printLine() {
    console.log('line ' + (i + 41));
    if (++i < process.stdout.columns) {
      setTimeout(printLine, 40);
    }
    else {
      clear();
      setTimeout(loop, 3000);
    }
  })()
})()

```

* The first line ensures the visible lines are always cleared.
* The second line ensures the scroll history is cleared.