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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Here is a non-exhaustive list, in execution-order, of what each file tends to contain:
.zshenvis always sourced. It often contains exported variables that should be available to other programs. For example,$PATH,$EDITOR, and$PAGERare often set in.zshenv. Also, you can set$ZDOTDIRin.zshenvto specify an alternative location for the rest of your zsh configuration..zprofileis for login shells. It is basically the same as.zloginexcept that it’s sourced before.zshrcwhereas.zloginis sourced after.zshrc. According to the zsh documentation, “.zprofileis meant as an alternative to.zloginfor ksh fans; the two are not intended to be used together, although this could certainly be done if desired.”.zshrcis for interactive shells. You set options for the interactive shell there with thesetoptandunsetoptcommands. 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)..zloginis for login shells. It is sourced on the start of a login shell but after.zshrc, if the shell is also interactive. This file is often used to start X usingstartx. Some systems start X on boot, so this file is not always very useful..zlogoutis sometimes used to clear and reset the terminal. It is called when exiting, not when opening.
You should go through the configuration files of random Github users to get a better idea of what each file should contain.
Method 2
Here a list of what each file should/shouldn’t contain, in my opinion:
.zshenv
[Read every time]
This file 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 because it may break some tools (by setting aliases for example).
.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 this file, you can apply the configuration updates by running a 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 .zprofile, but is read after .zshrc. You can consider the shell to be fully set up at .zlogin execution time
So, I use it to launch external commands which do not modify shell behaviors (e.g. a login manager).
.zlogout
[Read at logout][Within login shell]
Here, you can clear your terminal or any other resource which was setup at login.
How I choose where to put a setting
- if it is needed by a command run non-interactively:
.zshenv - if it should be updated on each new shell:
.zshenv - if it runs a command which may take some time to complete:
.zprofile - if it is related to interactive usage:
.zshrc - if it is a command to be run when the shell is fully setup:
.zlogin - if it releases a resource acquired at login:
.zlogout
Method 3
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).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0