This isn’t a brand new trick. It’s based on a Bash function for “alias last” that I’ve seen in a few places. The original version uses history
to get the last command you ran and turn it into a temporary alias. I expanded it a little with some escaping for quotes and trimming whitespace, but it’s the same function.
I wanted an easy way to get it into a more permanent place, though. So I wrote als
(“alias last and save”). You can just drop it in ~/.bash_profile
, edit in the filename to save to in the aliasfile=
line1, and run source ~/.bash_profile
. Next time you come up with a complex pipeline of commands to accomplish a task, just run als myawesomecommand
. A line will be appended to your alias file that turns myawesomecommand
into an alias that will be available every time you use your shell (it also goes ahead and creates it in the current session so you don’t have to source the file).
If you run it with “c” being the first argument before the alias name (als c NAME
), it will cut off the last argument in the command you’re saving. That’s for if the last argument is a filename or pattern and you’ll be changing it next time you want to run the command. Hacky, but handy if you remember it.
I’ve been starting to port over to zsh lately, but I’m still learning about a few quirks. In this case, I’m not certain why the history
command behaves differently. If I figure that out, this will work fine in zsh as well.
-
This file can be your
~/.bash_profile
file, or any other file, as long as yousource [FILENAME]
it in your main profile. ↩