Search code examples
zshzshrc

How to make zsh search configuration in $XDG_CONFIG_HOME


Looking to make my ~ a cleaner place, I would like to move as much user configuration files into $XDG_CONFIG_HOME, which is ~/.config by default. So I would like to store all my zsh user files in $XDG_CONFIG_HOME/zsh/. So far already have this:

% ls $XDG_CONFIG_HOME/zsh/
histfile  zsh_cache zshrc

Easy, you just have to fill your ~/.zshrc. Now the trickiest part seems to make zsh read directly $XDG_CONFIG_HOME/zsh/zshrc without sourcing it from ~/.zshrc. How would you proceed?


Solution

  • One may edit /etc/zsh/zshenv to set $XDG_CONFIG_HOME directories and $ZDOTDIR. This require write privilegies on this files though.

    So provided that $HOME is defined when zsh read it (I don't know if it's the case), you may add to your /etc/zsh/zshenv:

    if [[ -z "$XDG_CONFIG_HOME" ]]
    then
            export XDG_CONFIG_HOME="$HOME/.config/"
    fi
    
    if [[ -d "$XDG_CONFIG_HOME/zsh" ]]
    then
            export ZDOTDIR="$XDG_CONFIG_HOME/zsh/"
    fi