Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make zsh search configuration in $XDG_CONFIG_HOME

Tags:

zsh

zshrc

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?

like image 837
psychoslave Avatar asked Jan 16 '14 13:01

psychoslave


3 Answers

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
like image 124
psychoslave Avatar answered Oct 06 '22 22:10

psychoslave


It is good practice to not put a / at the end of any variable holding a certain path.

For example, $XDG_CONFIG_HOME/zsh translates to "$HOME/.config//zsh" and the / repeats because XDG_CONFIG_HOME ends with a /.

So I think your answer should be -

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
like image 29
nomad Avatar answered Oct 06 '22 22:10

nomad


Variation to psychoslave's answer which uses ${HOME}/.zshenv to initiate the environment. No root access needed.

export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:=${HOME}/.config}
export ZDOTDIR=${ZDOTDIR:=${XDG_CONFIG_HOME}/zsh}
source $ZDOTDIR/.zshenv

This was discussed on this thread on the zsh-users mailing list.

You may want to consider saving history in XDG_DATA_HOME. Specifications can be found at XDG Base Directory Specification.

like image 31
Friartek Avatar answered Oct 06 '22 23:10

Friartek