diff --git a/README.markdown b/README.markdown index 48e58f3..0fb7a75 100644 --- a/README.markdown +++ b/README.markdown @@ -44,6 +44,40 @@ To set a default Node version to be used in any new shell, use the alias 'defaul nvm alias default 0.6 +## Bash completion + +To activate, you need to source `bash_completion`: + + [[ -r $NVM_DIR/bash_completion ]] && . $NVM_DIR/bash_completion + +Put the above sourcing line just below the sourcing line for NVM in your profile (`.bashrc`, `.bash_profile`). + +### Usage + +nvm + + $ nvm [tab][tab] + alias copy-packages help list run uninstall version + clear-cache deactivate install ls unalias use + +nvm alias + + $ nvm alias [tab][tab] + default + + $ nvm alias my_alias [tab][tab] + v0.4.11 v0.4.12 v0.6.14 + +nvm use + + $ nvm use [tab][tab] + my_alias default v0.4.11 v0.4.12 v0.6.14 + +nvm uninstall + + $ nvm uninstall [tab][tab] + my_alias default v0.4.11 v0.4.12 v0.6.14 + ## Problems If you try to install a node version and the installation fails, be sure to delete the node downloads from src (~/nvm/src/) or you might get an error when trying to reinstall them again or you might get an error like the following: diff --git a/bash_completion b/bash_completion new file mode 100644 index 0000000..e24b394 --- /dev/null +++ b/bash_completion @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +# bash completion for Node Version Manager (NVM) + +__nvm_generate_completion() +{ + declare current_word + current_word="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=($(compgen -W "$1" -- "$current_word")) + return 0 +} + +__nvm_commands () +{ + declare current_word + declare command + + current_word="${COMP_WORDS[COMP_CWORD]}" + + COMMANDS='\ + help install uninstall use\ + run ls list deactivate alias\ + unalias copy-packages clear-cache version' + + if [ ${#COMP_WORDS[@]} == 4 ]; then + + command="${COMP_WORDS[COMP_CWORD-2]}" + case "${command}" in + alias) __nvm_installed_nodes ;; + esac + + else + + case "${current_word}" in + -*) __nvm_options ;; + *) __nvm_generate_completion "$COMMANDS" ;; + esac + + fi +} + +__nvm_options () +{ + OPTIONS='' + __nvm_generate_completion "$OPTIONS" +} + +__nvm_installed_nodes () +{ + __nvm_generate_completion "$(nvm_ls) $(__nvm_aliases)" +} + +__nvm_aliases () +{ + declare aliases + aliases="" + if [ -d $NVM_DIR/alias ]; then + aliases="`cd $NVM_DIR/alias && ls`" + fi + echo "${aliases}" +} + +__nvm_alias () +{ + __nvm_generate_completion "$(__nvm_aliases)" +} + +__nvm () +{ + declare previous_word + previous_word="${COMP_WORDS[COMP_CWORD-1]}" + + case "$previous_word" in + use|run|ls|list|uninstall) __nvm_installed_nodes ;; + alias|unalias) __nvm_alias ;; + *) __nvm_commands ;; + esac + + return 0 +} + +complete -o default -o nospace -F __nvm nvm