Shell – How to make shell aliases available when shelling out from Vim

aliasenvironment-variablesshellvim

In my .zshrc, I declare some aliases. Eg, which dbstart shows the contents of that alias.

If I open vim from that shell, it is a child process. In Vim, I can run shell commands with :! some_command. If I do :! echo $0 to see what shell Vim is using, it outputs '/bin/zsh'. However, my aliases are unavailable there.

How can I make my normal shell alises available when I shell out from Vim?

Two ideas:

  • Somehow EXPORT the aliases from the original shell, to its child process vim, and again to its shell child process
  • Configure Vim to read my .zshrc when creating its subshells

Best Answer

Old question, but the cleanest solution for vim in zsh was to add the alias to ~/.zshenv, the file that zsh loads for all shells, login, interactive, or otherwise. This avoids starting vim or zsh with flags and any possible problems with that.

There's a nice explanation of ~/.zshenv vs ~/.zshrc here: http://tanguy.ortolo.eu/blog/article25/shrc

Basically, zsh always sources ~/.zshenv. Interactive shells source ~/.zshrc, and login shells source ~/.zprofile and ~/.zlogin. Thus, an interactive login shell sources ~/.zshenv ~/.zprofile ~/.zlogin ~/.zlogin, and a noninteractive, nonlogin shell like the one vim uses to run commands only sources ~/.zshenv.

Related Question