Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active May 13, 2021 06:28
Show Gist options
  • Save ttscoff/6381380 to your computer and use it in GitHub Desktop.
Save ttscoff/6381380 to your computer and use it in GitHub Desktop.
Alias last command in bash and save
# 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
}
@brysmi
Copy link

brysmi commented Sep 12, 2013

neat

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