Skip to content

Instantly share code, notes, and snippets.

@runofthemill
Created March 3, 2020 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save runofthemill/5943f88410fe1c89b02bc3b8c29248c4 to your computer and use it in GitHub Desktop.
Save runofthemill/5943f88410fe1c89b02bc3b8c29248c4 to your computer and use it in GitHub Desktop.
Lazy loaded NVM version in iTerm2 Status Bar component

Source custom.zsh in your zsh profile, then in iTerm 2 go to Preferences -> Profiles -> Session, and under Miscellaneous check "Status bar enabled" then click "Configure Status Bar".

Add an Interpolated String component, then configure the String Value to be \(user.nodeVersion) (adjust to match the name of the variable set in iterm2_print_user_vars(), if different)

Now when you load a new session with the configured profile, the Status Bar will show "unset" until node is loaded from nvm, and will show the correct version whenever nvm use is called.

#!/bin/sh
# See https://www.iterm2.com/3.3/documentation-scripting-fundamentals.html
function iterm2_print_user_vars() {
iterm2_set_user_var nodeVersion $(set_node_version)
}
function set_node_version() {
if (type _zsh_nvm_nvm &>/dev/null); then
echo $(nvm current)
else
echo "unset"
fi
}
# if nvm isn't loaded, and user switches into a dir with .nvmrc, load nvm
function chpwd() {
if ! (type _zsh_nvm_nvm &>/dev/null); then
if [[ -e ".nvmrc" ]]; then
nvm use
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment