nvm icon indicating copy to clipboard operation
nvm copied to clipboard

Add an easy way to set up a custom prompt displaying node version

Open colearendt opened this issue 3 years ago • 4 comments

Related to #2849 and the pyenv project again. 😄

I can't say I feel very confident changing my shell custom prompt (PS1), or that I will like what I come up with. However, pyenv's integration did this for me, and I love it.

It would be nice for nvm to do the same. I'm sure there are some classy prompts out there in the world - even documenting best practice / recommended setup would be great. Better yet - a way to make this an easy addition for folks could be useful.

IIRC the way pyenv does it:

  • if there is a local .python_version file, display the version in the shell
(python-virtualenv-name) ➜  directory-name git:(branch) ✗
  • if not, do not display the version (so it does not clutter up other, non-python projects)

i.e. so I would expect this would only happen when the following message is displayed:

Found '/Users/colearendt/my/app/.nvmrc' with version <v16>

colearendt avatar Jul 27 '22 10:07 colearendt

It would be far too intrusive for nvm to do this for you - folks' prompts are usually highly customized, and messing with that is way more trouble than it's worth.

My personal prompt includes a \[\033[0;31m\]$(command type nvm >/dev/null 2>&1 && printf " " && nvm current)\[\033[0m\] which adds the colorized current nvm version, when present.

I don't think there's any appropriate shortcut for learning how to set your own PS1.

ljharb avatar Jul 30 '22 10:07 ljharb

Thanks for the reference! That makes sense. Is that something you'd be willing to add to documentation or accept a PR doing so? Or better to just leave for users to sort out themselves?

I did a bit more digging and found a bit of entertaining trivia:

  • this functionality actually seems to come from pyenv-virtualenv
  • this functionality has been deprecated for almost a decade? 😮 Not sure how I have missed this message for so long. 🤷

colearendt avatar Jul 30 '22 10:07 colearendt

I usually add PS1="(nvm-\$(nvm current)) $PS1" to my ~/.bashrc right after nvm activation scripts. It keeps any existing prompt set by other applications like Git, Python.

random-user-00 avatar Aug 06 '22 18:08 random-user-00

I think if there's a well-maintained guide to customizing your PS1, it's fine to link to it, but I also think that it's probably not something that someone should be doing if they also can't figure out how to Google for it :-)

ljharb avatar Aug 06 '22 20:08 ljharb