My question is actually two-fold; I have a user defined function for TeX processing, called texsed, which is as following:

function texsed () {
    sed -i -f $S/tex_rule.sed $1 && pdflatex $2 $1

this definition is in the zshrc file ~/.zshrc. Now, whenever I call which texsed from my zsh session, I get this output. The issue is that I cannot get the same output from vim, which I've tried to get by the following commands:

:r !which texsed
:r !zsh -c "which texsed"

neither of which worked.

I obviously do not understand the builtin command, so my question would (naively) be:

  1. How does the zsh builtin which work?
  2. How would I be able to get the definition of an alias or function defined through a config by such an external command?

Thanks in advance :)

  • A partial answer to my own question; sourcing the config and then executing the command seems to work: :r !zsh -c "source $HOME/.zshrc; which texsed" Commented Dec 17, 2022 at 17:36
  • 3
    Both of those are non-interactive invocations of zsh - I don't remember the rules for zsh, but it's possible it simply doesn't load your zshrc for non-interactive usage
    – muru
    Commented Dec 17, 2022 at 17:44
  • Dingdingding: @muru is right, as man zsh confirms: if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc; muru, would you post that as a quick answer? Commented Dec 17, 2022 at 18:21

1 Answer 1


When you use :!... in Vim, it typically runs $SHELL with the arguments -c and .... This results in a non-interactive shell. Sand from the zsh docs:

Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login shell, commands are read from /etc/zprofile and then $ZDOTDIR/.zprofile. Then, if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are read.

So, with :!..., .zshrc is simply not read at all, and the function isn't defined.

As I suggested in a related post on the Vim Stack Exchange, there're two ways to affect the invocation in :!... so that you can have this function defined:

  1. Add an option to the shell being used to run :! commands using shellcmdflag. From :h :!:

    On Unix the command normally runs in a non-interactive
    shell.  If you want an interactive shell to be used
    (to use aliases) set 'shellcmdflag' to "-ic".
  2. Environment variables. Now, if this were bash, there's a quick solution: $BASH_ENV. But for zsh, there's no simple equivalent. See Does zsh have any equivalent to BASH_ENV? for a way by setting ZDOTDIR. But I'd still prefer this way as you can add exactly the configuration you need when using :! in Vim.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .