Is it possible to define a configuration file for zsh scripts?

I am running macOS 10.15 and the default shell is zsh. I have defined, i.e., alias abc="echo yes" in ~/.zshrc, ~/.zsh_profile, ~/.profile, and ~/.zsh_login, every configuration file I can think of. However, a shell script t.sh calling abc will complain command not found: abc:

⇒  cat t.sh
⇒  zsh t.sh
t.sh:1: command not found: abc
⇒  sh t.sh
t.sh: line 1: abc: command not found

I understand that adding a line source ~/.zshrc or source ~/.zsh_login on the top of t.sh will work, but I wonder is it possible to set a configuration file that runs by default for any zsh script executed on my machine (by me)?

I'm asking this because I want to set an alias that works when I envoke a command inside vim via :!, e.g. :!abc, as well as the ] command in the program nnn. Also please let me know if this is irrelevant to what I asked above. I'm confused of the login shell and non-login shell, or other types of shell. FYI, in vim, :!echo $0 will return /bin/zsh and :!whoami will return ***(my name), so it seems like it's a login shell.

  • Welcome! You want a specific file to be sourced or commands available without specifying the source? Commented Sep 13, 2020 at 22:48
  • After googling about login and non-login shell, I understand that I'm talking about non-login shell, as echo $0 returns /bin/zsh, without a - in front. But, since .zshrc is sourced by a non-login shell, why is it not working? Or, is .zshrc only for interactive shell? What about non-interactive non-login shell?
    – Jordan He
    Commented Sep 14, 2020 at 3:54

1 Answer 1


For any zsh script you run, ~/.zshenv will be sourced:

=> cat ~/.zshenv
alias abc="echo yes"
=> cat t.sh
=> zsh t.sh

A simplified hierarchy of dotfiles for zsh:

  • ~/.zshenv - sourced for all zsh scripts.
  • ~/.zshrc - sourced for all interactive zsh sessions (those attached to a terminal). This is usually the best place to put aliases.
  • ~/.zprofile - sourced for a login zsh session, i.e. the first interactive session in a set.

For your usage, it may be preferable to configure MacVim to launch an interactive shell so it'll pick up aliases from ~/.zshrc. To do that, add this to your ~/.gvimrc (note the g) file:

let &shell='/bin/zsh -i'

For other vim flavors, you may have to do something a bit more complicated - see the answers here:

  • Thanks! Putting alias in ~/.zshenv works both in vim (via :!command) and nnn (] then command). That's all I need.
    – Jordan He
    Commented Sep 16, 2020 at 22:27

You must log in to answer this question.

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