Skip to content

Instantly share code, notes, and snippets.

@zerosrat
Created November 14, 2025 04:48
Show Gist options
  • Select an option

  • Save zerosrat/1f5478e69ad03ef367d487e6e069e0e5 to your computer and use it in GitHub Desktop.

Select an option

Save zerosrat/1f5478e69ad03ef367d487e6e069e0e5 to your computer and use it in GitHub Desktop.
fish auto nvm
# file path: ~/.config/fish/config.fish
# NVM 自动切换配置
# 查找.nvmrc文件的函数
function nvm_find_nvmrc
set -l path (pwd)
while test "$path" != "/"
if test -f "$path/.nvmrc"
echo "$path/.nvmrc"
return 0
end
set path (dirname "$path")
end
return 1
end
# NVM 自动切换函数
function nvm_auto_switch
set -l nvmrc_path (nvm_find_nvmrc)
if test -n "$nvmrc_path"
set -l nvmrc_version (cat "$nvmrc_path" | string trim)
set -l current_version (nvm current 2>/dev/null | string trim)
# 检查版本是否已安装
if nvm list | grep -q "$nvmrc_version"
set -l resolved_version (nvm version "$nvmrc_version" 2>/dev/null | string trim)
if test "$current_version" != "$resolved_version"
echo "🔄 Switching to Node.js $nvmrc_version (from .nvmrc)"
nvm use "$nvmrc_version"
end
else
echo "⚠️ Node.js version $nvmrc_version (from .nvmrc) is not installed"
echo "💡 Run: nvm install $nvmrc_version"
end
end
end
# 目录变化时触发检查
function __nvm_auto_switch_on_pwd --on-variable PWD
nvm_auto_switch
end
# 终端启动时执行一次检查
if status is-interactive
nvm_auto_switch
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment