Skip to content

Commit

Permalink
bash completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rafmagana committed Apr 9, 2012
1 parent 6e50bc0 commit cfa81b4
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.markdown
Expand Up @@ -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:
Expand Down
82 changes: 82 additions & 0 deletions 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

0 comments on commit cfa81b4

Please sign in to comment.