Easily create new CLI aliases with `new-alias`

I love stuff like this:

1-uE8FMv0eWNQChRKyBrWR8Q

It’s a handy little alias to create aliases for commands you just ran … an “alias-generating alias”; my inner geek rejoices 🙂

Here’s the code (add it to your .bash_profile):

new-alias() {
  local last_command=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')
  echo alias $1="'""$last_command""'" >> ~/.bash_profile
  . ~/.bash_profile
}

Usage: new-alias name-for-the-alias

An alias for new aliases →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.