Created
January 26, 2014 16:16
-
-
Save ericdke/8635119 to your computer and use it in GitHub Desktop.
Save last bash command in alias
This file contains 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=~/.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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works only with
bash
. I'm not sure but I think the first version was from Brett Terpstra.