Last active
April 2, 2021 13:22
-
-
Save Plutor/9090b0658106c5bd2acbeca3edb2c1d5 to your computer and use it in GitHub Desktop.
Staylias makes your aliases persist across bash sessions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Staylias makes your aliases persist across bash sessions. | |
# To install, save this as .bash_staylias in your home dir add this line to your bashrc: | |
# source "$HOME/.bash_staylias" | |
# | |
# How to use: | |
# $ alias foo=bar | |
# $ alias foo | |
# alias foo=bar | |
# | |
# $ alias foo=baz | |
# foo exists, `alias -f ...` to save | |
# $ alias -f foo=baz | |
# foo overwritten | |
# $ alias foo | |
# alias foo=baz | |
# | |
# $ unalias foo | |
# $ alias foo | |
stayliasfile=$(realpath ${BASH_SOURCE[0]}) | |
alias() { | |
touch "$stayliasfile" || return | |
case $# in | |
0) | |
echo 'usage: alias [-f] KEY=VAL' | |
return | |
;; | |
1) | |
;; | |
*) | |
if [[ $1 == "-f" ]]; then | |
force=1 | |
shift | |
elif [[ ${1:0:1} == "-" ]]; then | |
echo 'usage: alias [-f] KEY=VAL' | |
return | |
fi | |
;; | |
esac | |
keyval="$*" | |
key="${keyval%%=*}" | |
val="${keyval#*=}" | |
if [[ -z $key || -z $val ]] || echo $keyval | grep -qv '='; then | |
grep "^builtin alias $key=" "$stayliasfile" | sed -e 's/^builtin alias //' | |
return | |
fi | |
if [[ -z $force ]] && grep -q "^builtin alias $key=" "$stayliasfile"; then | |
echo "$key exists, \`alias -f ...\` to save" | |
return | |
fi | |
sed -i -e "s/^builtin alias $key=.*//" "$stayliasfile" | |
echo "builtin alias $key=$val" >> "$stayliasfile" | |
builtin alias $key=$val | |
[[ -n $force ]] && echo "$key overwritten" | |
} | |
unalias() { | |
key="$*" | |
sed -i -e "s/^builtin alias $key=.*//" "$stayliasfile" | |
builtin unalias $key | |
} | |
# ALIASES WILL BE ADDED BELOW HERE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment