Skip to content

Instantly share code, notes, and snippets.

@garthk
Last active December 30, 2024 06:02
Show Gist options
  • Save garthk/d493a29a87e86e5c9876753aa836b668 to your computer and use it in GitHub Desktop.
Save garthk/d493a29a87e86e5c9876753aa836b668 to your computer and use it in GitHub Desktop.
ZSH plugin for XDG Base Directory Specification environment variables
if case $(uname) in
Linux)
XDG_CONFIG_DIRS=/etc/xdg
XDG_BIN_HOME=~/.local/bin
XDG_CACHE_HOME=~/.cache
XDG_DATA_DIRS=/usr/local/share:/usr/share
XDG_CONFIG_HOME=~/.config
XDG_DATA_HOME=~/.local/share
XDG_RUNTIME_DIR=/run/user/$(id -u)
XDG_STATE_HOME=~/.local/state
;;
Darwin)
XDG_BIN_HOME=~/Library/'Application Support'/.local/bin
XDG_CACHE_HOME=$(getconf DARWIN_USER_CACHE_DIR)/.cache
XDG_CONFIG_DIR=/Library/'Application Support'/.config
XDG_CONFIG_HOME=~/Library/'Application Support'/.config
XDG_DATA_DIRS=/Library/'Application Support'/.local/share:/usr/local/share:/usr/share
XDG_DATA_HOME=~/Library/'Application Support'/.local/share
XDG_RUNTIME_DIR=$(getconf DARWIN_USER_TEMP_DIR)
XDG_STATE_HOME=~/Library/'Application Support'/.local/state
;;
*)
echo >&2 "$(uname) is not yet supported."
false
;;
esac then
typeset -xUT XDG_CONFIG_DIRS xdg_config_dirs :
typeset -xUT XDG_DATA_DIRS xdg_data_dirs :
typeset -x XDG_BIN_HOME XDG_CACHE_HOME XDG_CONFIG_HOME XDG_DATA_HOME XDG_RUNTIME_DIR XDG_STATE_HOME
mkdir -p "$XDG_BIN_HOME" "$XDG_CACHE_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME"
fi
@garthk
Copy link
Author

garthk commented Aug 30, 2023

Sets variables according to the XDG Base Directory Specification. Tailored for macOS with reference to File System Basics. See in particular its advice regarding ~/Library/Preferences that:

You should not create files in this directory yourself

… hence XDG_CONFIG_HOME not using it.

Do let me know if you've any references that'll guide changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment