Skip to content

Instantly share code, notes, and snippets.

@ericdke
Created January 26, 2014 16:16
Show Gist options
  • Save ericdke/8635119 to your computer and use it in GitHub Desktop.
Save ericdke/8635119 to your computer and use it in GitHub Desktop.
Save last bash command in alias
# 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=~/.bashrc
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
}
@ericdke
Copy link
Author

ericdke commented Jan 26, 2014

Works only with bash. I'm not sure but I think the first version was from Brett Terpstra.

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