16

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?

1
  • AFAIK there is not autocomplete option for the v8 terminal. Maybe tools such as with-readline can be helpful to you, however. Commented Aug 17, 2011 at 17:19

2 Answers 2

12

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)
}
Sign up to request clarification or add additional context in comments.

2 Comments

this method didn't work for me. What worked better is to provide a "completer" function in the options for repl.start({}) - nodejs.org/api/repl.html#repl_repl_start_options
^ I had to use the callback version for it to work, but it does work.
9

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.