Skip to content

Instantly share code, notes, and snippets.

@romgrk
Last active December 25, 2015 08:12
Show Gist options
  • Save romgrk/e6b2f3299d52f544f732 to your computer and use it in GitHub Desktop.
Save romgrk/e6b2f3299d52f544f732 to your computer and use it in GitHub Desktop.
ZSH_ALIASES=${ZSH_ALIASES:-"$HOME/.aliases"}
# CMD and UNCMD are permanent alias
# FIXME: multiple 'cmd' create multiple entries (but only the last is sourced)
# 'uncmd' remove all entries
function cmd () {
if [[ $# == 0 ]]; then
sed -n '/^alias/ p' <$ZSH_ALIASES
return 0
elif [[ $# == 1 ]]; then
awk '
BEGIN { found = 0; }
/^alias '$1'=/ {
match($0, /=(.*)/, arr)
val = arr[1]
print "cmd \x1b[32m'$1'\x1b[0m =\x1b[38;5;27m", val,
"\t\x1b[38;5;246m(at line ", NR, " in file '$ZSH_ALIASES')\x1b[0m";
found = 1;
exit 0;
}
END { if (found == 0) { print "cmd \x1b[91m'$1'\x1b[0m not found"; } }
' <$ZSH_ALIASES
return 0
fi
if [[ -w $ZSH_ALIASES ]]; then
local aliascmd
if [[ $# == 2 ]]; then
aliascmd='alias '$1'="'${@:2}'"'
else
cmdval="${(V)@:2}"
aliascmd="alias $1='$cmdval'"
fi
print ${aliascmd/alias/cmd}
echo $aliascmd >> $ZSH_ALIASES
eval $aliascmd
return 0
else
print "Cant write file $ZSH_ALIASES"
return 2
fi
}
function uncmd () {
lnum=$(sed -n "/^alias $1=/ {=}" <$ZSH_ALIASES | paste -sd "," - )
if [[ -n $lnum ]]; then
echo -e 'uncmd \x1b[93m'$1' \x1b[38;5;246m(line '$lnum')'
unaliased=$(sed "/^alias $1=/ d" <$ZSH_ALIASES)
echo $unaliased >$ZSH_ALIASES
return 0
else
echo -e 'cmd \x1b[91m'$1'\x1b[0m not found'
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment