Introduction
If you find yourself constantly running the same long commands on your terminal, setting up quick aliases can be a game-changer. This blog post will walk you through creating two handy ZSH functions that allow you to define and save aliases on-the-fly. With these, you can alias any command, including the last command you ran, without even leaving the terminal!
1. Create Aliases On-the-Fly
First up, let's talk about how you can instantly create aliases for any given command.
Code Snippet:
add_alias() {
alias_name="$1"
alias_command="$2"
aliases_file="$HOME/.zsh_aliases"
# Create alias and save to the current session
alias "$alias_name=$alias_command"
# Append the alias to zsh_aliases file for persistence
echo "alias $alias_name=\"$alias_command\"" >> "$aliases_file"
# Source the zsh_aliases file to apply changes
source "$aliases_file"
echo "Alias $alias_name added."
}
Usage:
Place this function in your .zshrc file or another file that's sourced by it. Then add source $HOME/.zsh_aliases in your .zshrc to load these aliases in future sessions.
add_alias ll "ls -la"
Now, ll will be an alias for ls -la, and it will also be saved in your file for future use..zsh_aliases
2. Alias the Last-Run Command
Ever ran a command and immediately realized you want to alias it? This function has got you covered.
Code Snippet:
# Obviously feel free to name this command whatever you like. Same for the one above.
alias_last_command() {
alias_name="$1"
last_command=$(fc -ln -1)
last_command="${last_command##*( )}"
aliases_file="$HOME/.zsh_aliases"
if [ -z "$alias_name" ]; then
echo "Alias name is required."
return 1
fi
# Create the alias and save to the current session
alias "$alias_name=$last_command"
# Append the alias to .zsh_aliases file for persistence
echo "alias $alias_name=\"$last_command\"" >> "$aliases_file"
# Source the .zsh_aliases file to apply changes
source "$aliases_file"
echo "Alias $alias_name for '$last_command' added."
}
Usage:
Just like the previous function, add this to your .zshrc and make sure you're sourcing the file..zsh_aliases
To create an alias for the last command you ran:
alias_last_command ll
This will make ll an alias for whatever your last command was, saving it in for future sessions..zsh_aliases
Conclusion
With these two ZSH functions, you can quickly and conveniently set up aliases right from your terminal, making your workflow much more efficient. Give them a try and watch your productivity soar!
Happy coding! π
βΆ RELATED POSTS
Β» The Power Of Choosing Optimism
"The happiness of your life depends upon the quality of your thoughts." - Marcus Aurelius My first memories of childhood are grey, not in the cool, noir style of 1920s movies, but in the bleak, post-communist greyness of 1990s Romania. I was surrounded by people who had hoped for freedom for so
Β» Lessons in cybersecurity, Part I
Here's a little story from the trenches, from far far away when I was a kiddo learning my way through webservers, PHP and vulnerable (pirated) bulletin boards software. ππ» Intro A long time ago around 200* something, I was really interested in game hacking related topics and somehow I got in...
Β» Introducing goqueuelite: Golang + SQLite queue
It finally happened! I am about to introduce my first proper open source project, it is calledΒ squeueliteΒ and it is a Golang package that tries to fix the queue issue using SQLite only. The package can be found out github.com/risico/goqueuelite, check it out. The package is not production ready...
βΆ COMMENTS
Comments from this blog and Bluesky
No comments yet. Be the first to comment!