diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md new file mode 100644 index 000000000..1a9258e4f --- /dev/null +++ b/plugins/nvm/README.md @@ -0,0 +1,31 @@ +# NVM + +The `nvm` plugin locates and loads [NVM](https://github.com/creationix/nvm) if it is installed on this system. It also provides completion support, either through its own completion definition, or the `bash_completion` shipped with NVM itself. + +### Installation Locations + +This plugin looks in a few well-known locations for the NVM installation, in the following order: + +* `$NVM_DIR` - Lets user force loading from an arbitrary location +* `~/.nvm` - Default installation location for NVM +* `$(brew --prefix nvm)` - Mac Homebrew's NVM installation location + +After NVM is loaded, `$NVM_DIR` is set as part of NVM initialization, so you can use it regardless of whether you set it explicitly earlier. + +Note that the NVM installer adds the commands to load NVM (by `source`ing it from the install location) directly to your `~/.zshrc` or other initialization file. This is not required for NVM to be "installed" in the sense that this plugin means. This plugin provides an alternate way of loading NVM, and you shouldn't use both. If you use this plugin, remove any `source ~/.nvm/nvm.sh` or similar lines from your `~/.zshrc`. + +Other package managers may install NVM to other locations, and we'll add those in if you let us know about them by [opening an issue or PR on GitHub](https://github.com/robbyrussell/oh-my-zsh/issues/new). + +### Completion Alternatives + +This plugin supplies a zsh-native `_nvm` completion definition, which is loaded by default. This provides descriptions of the options it can complete. + +You can also use the portable `bash` completion definition that is bundled with the NVM installation. This has the advantages: +* More complete with respect to options (because you're using the exact completion definitions for the version of NVM you're running). +* Able to complete Node.js versions based on the versions installed in the NVM you have loaded. + +To use NVM's bundled bash completions, set `ZSH_NVM_BUNDLED_COMPLETION=true` in your `~/.zshrc` before loading Oh My Zsh. + +### Environment Variables + +* `$ZSH_NVM_BUNDLED_COMPLETION` - if `true`, uses NVM's own completion definition instead of the OMZ nvm plugin's definition. diff --git a/plugins/nvm/_nvm b/plugins/nvm/_nvm index 1414dcbb1..8d052172b 100644 --- a/plugins/nvm/_nvm +++ b/plugins/nvm/_nvm @@ -10,8 +10,11 @@ _1st_arguments=( 'uninstall:uninstall a version' 'use:modify PATH to use version' 'run:run version with given arguments' + 'current:display activated version' 'ls:list installed versions or versions matching a given description' 'ls-remote:list remote versions available for install' + 'version:resolve given description to a single local version' + 'version-remote:resolve given description to a single remote version' 'deactivate:undo effects of NVM on current shell' 'alias:show or set aliases' 'unalias:deletes an alias' diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh index 9dde3a266..c3659629d 100644 --- a/plugins/nvm/nvm.plugin.zsh +++ b/plugins/nvm/nvm.plugin.zsh @@ -1,5 +1,40 @@ -# Set NVM_DIR if it isn't already defined -[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm" +# nvm +# +# This plugin locates and loads nvm, looking for it in well-known locations. -# Load nvm if it exists -[[ -f "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh" +() { + emulate -L zsh + local nvm_dir="" dir install_locations + if [[ -n $NVM_DIR ]]; then + nvm_dir=$NVM_DIR + else + # Well-known common installation locations for NVM + install_locations=( ~/.nvm ) + # Mac Homebrew sticks + which brew &>/dev/null && install_locations+=$(brew --prefix nvm) + for dir ($install_locations); do + if [[ -s $dir/nvm.sh ]]; then + nvm_dir=$dir + break + fi + done + fi + + if [[ -n $nvm_dir ]]; then + source $nvm_dir/nvm.sh + fi + + # Locate and use the completion file shipped with NVM, instead of this + # plugin's completion + # (Their bash completion file has zsh portability support) + if [[ $ZSH_NVM_BUNDLED_COMPLETION == true ]]; then + local bash_comp_file + # Homebrew relocates the bash completion file, so look multiple places + for bash_comp_file ( bash_completion etc/bash_completion.d/nvm ); do + if [[ -s $NVM_DIR/$bash_comp_file ]]; then + source $NVM_DIR/$bash_comp_file + break; + fi + done + fi +}