Last active
April 14, 2022 22:32
-
-
Save ColinRyan/d7c803a5fcb3a006e5b4d73234368216 to your computer and use it in GitHub Desktop.
bash snippets for working with aliases
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
# Requires bash, history, tail, cut, echo, and sed (if you have bash you probably | |
# don't need to install anything). | |
# Takes a alias name and gets the last command from the history. Makes it an | |
# alias and puts it in .bash_aliases. Be sure to source .bash_aliases in .bashrc | |
# or this wont work. | |
makeAlias() | |
{ | |
if [ $# -eq 0 ]; then | |
echo "No arguments supplied. You need to pass an alias name" | |
else | |
newAlias=$(history | tail -n 2 | cut -c 8- | sed -e '$ d') | |
escapedNewAlias=${newAlias//\'/\'\\\'\'} | |
echo "alias $1='${escapedNewAlias}'" >> ~/.bash_aliases | |
. ~/.bashrc | |
fi | |
} |
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
# Requires bash,fzf,awk,fc,echo,and cat to run (you're probably missing fzf). | |
# Make a function from history and put it in .bash_functions named after w/e is in $1. | |
makeMacro () { | |
if [ $# -eq 0 ]; then | |
echo "No arguments supplied. You need to pass a name" | |
else | |
commands="$(fc -rl -40 | fzf -m | awk -F $'\t ' '{print $2}')" | |
if [ -z "$commands" ] | |
then | |
echo "No commands selected. Be sure to use Tab to select sections | |
of your history and enter when you're done." | |
else | |
# ugly block but it doesn't seem to work any other way | |
cat <<EOF >> ~/.bash_functions | |
$1 () { | |
$commands | |
} | |
EOF | |
echo $out | |
. ~/.bashrc | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I saw redo on Hacker News and realized it looked familiar to a bash script I wrote years ago powered by FZF so I'm shamelessly sharing this for internet points.
IMO, Redo and my script are 'fine' but, day to day, I make aliases from the last command I run a lot more often. Enjoy!