Last active
February 20, 2025 09:52
-
-
Save ttscoff/6381380 to your computer and use it in GitHub Desktop.
Alias last command in bash and save
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
# alias last and save | |
# use `als c NAME` to chop off the last argument (for filenames/patterns) | |
als() { | |
local aliasfile chop x | |
[[ $# == 0 ]] && echo "Name your alias" && return | |
if [[ $1 == "c" ]]; then | |
chop=true | |
shift | |
fi | |
aliasfile=~/.bash_it/aliases/custom.aliases.bash | |
touch $aliasfile | |
if [[ `cat "$aliasfile" |grep "alias ${1// /}="` != "" ]]; then | |
echo "Alias ${1// /} already exists" | |
else | |
x=`history 2 | sed -e '$!{h;d;}' -e x | sed -e 's/.\{7\}//'` | |
if [[ $chop == true ]]; then | |
echo "Chopping..." | |
x=$(echo $x | rev | cut -d " " -f2- | rev) | |
fi | |
echo -e "\nalias ${1// /}=\"`echo $x|sed -e 's/ *$//'|sed -e 's/\"/\\\\"/g'`\"" >> $aliasfile && source $aliasfile | |
alias $1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
neat