Shell aliases in vim command mode

I have a bunch of alliases in my shell . When coding in vim, I frequently run whatever file I'm working on from vim's command mode,

:!node %

often by just mapping a key combo to run it.

:map ,r :w\|!node %<CR>

A problem I often run into is that vim's command mode doesn't know about my shell aliases. So if I have an alias like this in .zshrc

alias python=python3

When running :!python % from vim, I get something like

zsh:1: command not found: python

shell returned 127

My aliases used to be defined in .zshrc, but this file is not sourced when running a command from vim's command mode. The solution is to move your aliases to ~/.zshenv , which is sourced.

Note there's no need to load ~/.zshenv from ~/.zshrc, as it's part of the default zsh startup sequence and therefore loaded by default.

← Home