I want to add tab completion to a Nodejs CLI app (And preferably generate the tab completion dynamically).
I found a few npm modules but not sure how to really implement them:
https://github.com/hij1nx/complete
https://github.com/mklabs/node-tabtab
So what I am looking for is so I can have a nodejs file that is something like:
my-cmd create arg1 arg2
But then I might want to autocomplete like:
my-cmd cr<tab> -> create
Thanks!
Use omelette package that I built. If you have any questions, please contact me.
After I answered, I kept reading tabtab source a bit and noticed that I can also run
pkgname completion install
to install the completion. since my environment was already dirty, I don't know if it actually did anything, but seems to me like it did..
@CameronLittle has given great documentation.
For the impatient, you can start by running
sudo bash -c 'pkgname completion > /etc/bash_completion.d/pkgname' source /etc/bash_completion.d/pkgname
This will add completion to your current bash session.
As far as I know, new sessions will get the completion automatically.
To make the process seamless for user, you can use the install
and postinstall
hooks in package.json
https://docs.npmjs.com/misc/scripts
Make sure to not print anything by default. means running pkgname
should result in no output, or otherwise it will not work.
It seems tabtab has an annoying bug that was resolved in master but never got into a release..
The relevant commit to fix it is this: https://github.com/mklabs/node-tabtab/commit/f8473555bf7278a300eae31cbe3377421e2eeb26
which handles completion for strings starting with --
.
The commit if from february 2014, however the latest release as of (Jan. 2015) is 0.0.2 from Jan. 2014.. I assume there will not be more releases.
So if you want to get this fix, and you should(!), install tabtab only from master.
don't waste 2 hours figuring out what you did wrong like me :)
While @CameronLittle's answer gives the explanation behind the scene, I would like to explain how to I reached the answer.
I tried using the package tabtab
which has an explicit section about installing it. see https://www.npmjs.com/package/tabtab#completion-install
However, that didn't seem to work for me.
Looking at the code they instruct to add, I see the following process.argv.slice(2)[0] === 'completion'
which made me run the command pkgname completion
, which outputs something that starts with
###-begin-pkgname-completion-### ### credits to npm, this file is coming directly from isaacs/npm repo # # Just testing for now. (trying to learn this cool stuff) # # npm command completion script # # Installation: pkgname completion >> ~/.bashrc (or ~/.zshrc) #
the words this file is coming directly from isaacs/npm repo
made me wonder more. following the other answer here, I looked at /etc/bash_completion.d/npm - which showed the same exact content.. and so the comment.
I decided to run
pkgname completion > /etc/bash_completion.d/pkgname
however that requires sudo permissions and so becomes
sudo bash -c "pkgname completion > /etc/bash_completion.d/pkgname
and then, in order to apply it to current bash session I had to run
source /etc/bash_completion.d/pkgname
and voila! it works!
when I tried to open another terminal, it still worked, so I assume it will apply to all users. if not - you should add it to .bashrc or something..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With