[zsh] Configure ls(1) colors with environment variables instead of aliases

ls(1) on macOS and FreeBSD is the BSD version of the utility. As such it can be
configured to display directory contents in color using a command line argument
(-G) or environment variables (LSCOLORS). Using environment variables is nice
because it'll work regardless of the arguments you pass, so there's no need to
configure aliases.
This commit is contained in:
Eryn Wells 2024-11-12 13:47:20 -08:00
parent 070f8f9a71
commit 329db6f6de

View file

@ -3,23 +3,23 @@
function init-rc-ls function init-rc-ls
{ {
local ls_options='--color=auto'
alias ls="ls $ls_options"
alias la="ls -A $ls_options" alias la="ls -A $ls_options"
alias ll="ls -l $ls_options" alias ll="ls -l $ls_options"
alias l.="ls -d $ls_options .*" alias l.="ls -d $ls_options .*"
local dircolors_bin=$(whence -p dircolors || whence -p gdircolors) # Enable ls colors
if [[ -n "$dircolors_bin" ]]; then export CLICOLOR=1 COLORTERM=1
local dircolors_config
if [[ -f "$HOME/.dircolors/$SYS.cfg" ]]; then
dircolors_file="$HOME/.dircolors/$SYS.cfg"
else
dircolors_file="$HOME/.dircolors/default.cfg"
fi
if [[ -f "$dircolors_config" ]]; then # Define colors for ls. See the LSCOLORS documentation in ls(1).
eval $($dircolors_bin $dircolors_config) # The default is "exfxcxdxbxegedabagacadah".
export LSCOLORS=Exdxcxfxbxegedabagacadah
local dircolors_bin=$(whence -p dircolors || whence -p gdircolors)
if [[ -x "$dircolors_bin" ]]; then
if [[ -f "$HOME/.dircolors/$SYS.cfg" ]]; then
eval $dircolors_bin "$HOME/.dircolors/$SYS.cfg"
elif [[ -f "$HOME/.dircolors/default.cfg" ]]; then
eval $dircolors_bin "$HOME/.dircolors/default.cfg"
fi fi
fi fi
} }