A simple Zsh alias management tool that helps you quickly save and persist aliases, ensuring that your custom aliases remain available even after restarting the terminal.
English | 简体中文
- Save Aliases: Save defined aliases to a file to ensure they remain available in future terminal sessions.
- Load Aliases: Load aliases from the saved file, automatically adding them to the current session.
Add the following code to your ~/.zshrc file:
# Function to save a specific alias
function alias_save {
# Check if the alias exists
if alias "$1" > /dev/null 2>&1; then
# Extract the alias definition and ensure the correct format
local alias_definition=$(alias "$1" | sed "s/^alias //")
# Append the alias to the ~/.zsh_aliases file with alias prefix
echo "alias $alias_definition" >> ~/.zsh_aliases
echo "Alias $1 saved to ~/.zsh_aliases"
else
echo "Alias $1 does not exist"
fi
}
# Function to load aliases
function load_aliases {
if [ -f ~/.zsh_aliases ]; then
source ~/.zsh_aliases
echo "Aliases loaded from ~/.zsh_aliases"
else
echo "No alias file found."
fi
}
# Automatically load aliases on shell startup
load_aliasesuse curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/foolgry/zsh_alias/main/install.sh)"
use wget
sh -c "$(wget -qO- https://raw.githubusercontent.com/foolgry/zsh_alias/main/install.sh)"
Reload your Zsh configuration file to automatically load the saved aliases:
source ~/.zshrcFirst, define one or more aliases:
alias a='ls -lah'
alias b='git status'
alias c='git push'Save one of the aliases using the alias_save command, for example:
alias_save b # Save alias b to ~/.zsh_aliases fileCheck the contents of the ~/.zsh_aliases file:
cat ~/.zsh_aliasesThe output should include:
alias b='git status'Open a new Zsh session, or run the following command in the current session:
b # Run 'git status'Make sure you have added the load_aliases function to your ~/.zshrc file and that it is being called each time you start a terminal session.
Currently, this tool only supports saving and loading aliases. To delete a saved alias, manually edit the ~/.zsh_aliases file to remove the corresponding alias line, then reload the configuration:
source ~/.zshrcWe welcome contributions! If you have new feature requests or find any bugs, please submit an issue or a pull request.
This project is open-sourced under the MIT license. See the LICENSE file for details.
With this simple alias management tool, you can easily manage and persist your aliases in Zsh, making your development environment more efficient and personalized.