117

I'm looking for guidelines on what one should and should not include in the various startup files for zsh.

I understand the order of sourcing of these files, and the conditions under which they are sourced, but it is still not clear to me what should go in each.

142

Here is a non-exclusive list of what each file tends to contain:

  • Since .zshenv is always sourced, it often contains exported variables that should be available to other programs. For example, $PATH, $EDITOR, and $PAGER are often set in .zshenv. Also, you can set $ZDOTDIR in .zshenv to specify an alternative location for the rest of your zsh configuration.
  • .zshrc is for interactive shell configuration. You set options for the interactive shell there with the setopt and unsetopt commands. You can also load shell modules, set your history options, change your prompt, set up zle and completion, et cetera. You also set any variables that are only used in the interactive shell (e.g. $LS_COLORS).
  • .zlogin is sourced on the start of a login shell. This file is often used to start X using startx. Some systems start X on boot, so this file is not always very useful.
  • .zprofile is basically the same as .zlogin except that it's sourced directly before .zshrc is sourced instead of directly after it. According to the zsh documentation, ".zprofile is meant as an alternative to `.zlogin' for ksh fans; the two are not intended to be used together, although this could certainly be done if desired."
  • .zlogout is sometimes used to clear and reset the terminal.

You should go through the configuration files of random Github users to get a better idea of what each file should contain.

  • 3
    Just as a point of clarity, .zlogin is sourced after .zshrc (see man zshall for details). – Aaron Mar 31 '15 at 16:09
  • 7
    Be aware when setting $PATH in .zshenv, various other files all are sourced after this file that will override this value. See zsh.org/mla/users/2003/msg00600.html. – Beau Barker Oct 6 '15 at 0:19
  • Where should $LANG and $LC_ALL go? – Lenar Hoyt Nov 24 '15 at 19:41
  • 17
    Just for my own notes / confirmation and to help anybody else, the ultimate order is .zshenv → [.zprofile if login] → [.zshrc if interactive] → [.zlogin if login] → [.zlogout sometimes]. – Gabriel L. May 18 '16 at 20:48
14

Here a list of what each file should/shouldn't contain, in my opinion:

.zshenv

[Read every time]

It is always sourced, so it should set environment variables which need to be updated frequently. PATH (or its associated counterpart path) is a good example because you probably don't want to restart your whole session to make it update. By setting it in that file, reopening a terminal emulator will start a new Zsh instance with the PATH value updated.

But be aware that this file is read even when Zsh is launched to run a single command (with the -c option), even by another tool like make. You should be very careful to not modify the default behavior of standard commands as it may break some tools which use them (by setting aliases for example). For sure, it is not forbidden as you know what you are doing.

.zprofile

[Read at login]

I personally treat that file like .zshenv but for commands and variables which should be set once or which don't need to be updated frequently:

  • environment variables to configure tools (flags for compilation, data folder location, etc.)
  • configuration which execute commands (like SCONSFLAGS="--jobs=$(( $(nproc) - 1 ))") as it may take some time to execute.

If you modify that file, you can get the configuration updates by replacing the current shell with a new one as login shell:

exec zsh --login

.zshrc

[Read when interactive]

I put here everything needed only for interactive usage:

  • prompt,
  • command completion,
  • command correction,
  • command suggestion,
  • command highlighting,
  • output coloring,
  • aliases,
  • key bindings,
  • commands history management,
  • other miscellaneous interactive tools (auto_cd, manydots-magic)...

.zlogin

[Read at login]

This file is like .zshprofile, but is read after .zshrc. I consider the shell to be fully set up at this time.

So, I use it to launch external commands which do not modify the shell behaviors (e.g. a login manager).

.zlogout

[Read at logout][Within login shell]

Here, you can clear your terminal or any other resource setup at login.

How I choose where to put a setting

  • it is needed by a command run non-interactively: .zshenv
  • it should be updated on new shell: .zshenv
  • it runs a command which may take some time to complete: .zprofile
  • it is related to interactive usage: .zshrc
  • it is a command to be run when the shell is fully setup: .zlogin
  • it releases a resource acquired at login: .zlogout
0

Don't put ssh-agent calls in .zshenv. It caused my less command to not display any text files anymore, probably because its options were customized by Prezto (export LESS='-F -g -i -M -R -S -w -X -z-4' in .zprofile).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.