Make RVM's Ruby Available to Emacs Shell Commands

No matter if you use exec-path-from-shell or not, Emacs will not be able to know your RVM-managed Ruby information. This drove me crazy.

Most Emacs shell commands are invoked in an “inferior” mode, aka a “dumb” shell. This includes M-!, M-x shell, and also the projectile compile commands. That’s when some of your user login scripts will not automatically load, like the entirety of rvm, the Ruby Version Manager.

To add your default Ruby to the PATH environment via rvm in a way that Emacs can understand from the get-go, add this to your .zshrc/.bashrc – or after wherever you export PATH:

[[ "${TERM}" == "dumb" ]] && source $(rvm default do rvm env --path)

Or, if you prefer longer versions:

if [[ "${TERM}" == "dumb" ]]; then
    source $(rvm default do rvm env --path)
fi

# or

case $TERM in
  dumb*)
    # Emacs inferior shell is dumb
    source $(rvm default do rvm env --path)
    ;;
  xterm*)
    # ... handle xterm* here ...
    ;;
esac

That will execute the rvm initialization script and make the default ruby’s PATH available to dumb shells. The $TERM environment variable will probably be xterm-256color or similar when you use a regular terminal emulator.

I managed to survive until now because I discovered that I can build my website from Emacs with projectile by invoking rvm default do bundle exec nanoc compile instead of just nanoc compile. You might want to keep the rvm default do <CMD> handy for an emergency, too.