Dr McKay
Dr McKay

Reputation: 2568

How do I implement tab completion in node.js shell?

I was looking for this feature in node.js and I haven't found it.
Can I implement it myself? As far as I know, node.js doesn't load any file at it's startup (like Bash does with .bashrc) and I haven't noticed any way to somehow override shell prompt.

Is there a way to implement it without writing custom shell?

Upvotes: 17

Views: 5741

Answers (2)

thejh
thejh

Reputation: 45578

You could monkey-patch the REPL. Note that you must use the callback version of the completer, otherwise it won't work correctly:

var repl = require('repl').start()
var _completer = repl.completer.bind(repl)
repl.completer = function(line, cb) {
  // ...
  _completer(line, cb)
}

Upvotes: 13

jiyinyiyong
jiyinyiyong

Reputation: 4713

Just as a reference.

readline module has readline.createInterface(options) method that accepts an optional completer function that makes a tab completion.

function completer(line) {
  var completions = '.help .error .exit .quit .q'.split(' ')
  var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })
  // show all completions if none found
  return [hits.length ? hits : completions, line]
}

and

function completer(linePartial, callback) {
  callback(null, [['123'], linePartial]);
}

link to the api docs: http://nodejs.org/api/readline.html#readline_readline_createinterface_options

Upvotes: 9

Related Questions