export ALIASES_FILE="$XDG_CONFIG_HOME"/aliasesrc function mal { # Use fzf to allow the user to select an alias from the aliases file function _execute_alias { local alias_to_execute=$(cut -d' ' -f2- "$ALIASES_FILE" | fzf) # Check if the user cancelled the selection test -z $alias_to_execute && return 1 echo "Executing alias: $alias_to_execute" # Execute the selected alias eval ${alias_to_execute%%=*} return 0 } # Delete an existing alias by selecting it from a list function _interactive_delete { local alias_to_delete=$(cut -d' ' -f2- "$ALIASES_FILE" | fzf) # Check if the user cancelled the selection test -z $alias_to_delete && return 1 echo "Deleted alias: $alias_to_delete" # Delete the selected alias from the aliases file sed -i "/^alias ${alias_to_delete%%=*}=/d" "$ALIASES_FILE" unalias ${alias_to_delete%%=*} } # Delete an existing alias using specified name function _parameter_delete { local alias_to_delete=$1 local line_with_alias=$(grep "$1=" "$ALIASES_FILE") # Check if the alias exists in the aliases file if test -z "$line_with_alias"; then echo "No alias with the name $1 found." return 2 fi echo "Deleted alias: $alias_to_delete" # Delete the selected alias from the aliases file sed -i "/^alias ${alias_to_delete%%=*}=/d" "$ALIASES_FILE" unalias ${alias_to_delete%%=*} } # Rename an existing alias by selecting it from a list function _interactive_rename { local alias_to_rename=$(cut -d' ' -f2- "$ALIASES_FILE" | fzf) # Check if the user cancelled the selection test -z $alias_to_rename && return 1 echo "Input new name:" read new_name echo "Renaming alias from $alias_to_rename to $new_name" sed -i "s/^alias ${alias_to_rename%%=*}=/alias ${new_name}=/g" "$ALIASES_FILE" unalias ${alias_to_rename%%=*} } # Rename an existing alias using specified old and new names function _parameter_rename { local old_alias_name=$1 local new_alias_name=$2 local line_with_alias=$(grep "^alias ${old_alias_name}=" "$ALIASES_FILE") if test -z "$line_with_alias"; then echo "No alias with the name $old_alias_name found." return 1 fi echo "Renaming alias from $old_alias_name to $new_alias_name" sed -i "s/^alias ${old_alias_name}=/alias ${new_alias_name}=/g" "$ALIASES_FILE" unalias ${old_alias_name} } # Change the command associated with an existing alias by selecting it from a list function _interactive_command_change { local alias_to_change=$(cut -d' ' -f2- "$ALIASES_FILE" | fzf) # Check if the user cancelled the selection test -z $alias_to_change && return 1 echo "Input new command:" read new_command echo "Changing command for alias ${alias_to_change%%=*} to \"$new_command\"" sed -i "/^alias ${alias_to_change%%=*}=/{s/=.*/=\"$new_command\"/}" "$ALIASES_FILE" } # Change the command associated with an existing alias using specified name and command function _parameter_command_change { local alias_to_change=$1 local new_command="${@:2}" local line_with_alias=$(grep "$alias_to_change=" "$ALIASES_FILE") if test -z "$line_with_alias"; then echo "No alias with the name $alias_to_change found." return 1 fi echo "Changing command for alias $alias_to_change to \"$new_command\"" sed -i "/^alias ${alias_to_change}=/{s/=.*/=\"$new_command\"/}" "$ALIASES_FILE" } # Add a new alias function _add_command { local line_with_alias=$(grep "$1=" "$ALIASES_FILE") if test ! -z "$line_with_alias"; then echo "Alias found: $line_with_alias; change alias by using the -c flag." return 1 fi local lh="alias $1" local rh=\"${@:2}\" local alias_str="$lh=$rh" echo $alias_str >>"$ALIASES_FILE" echo "added '$alias_str' to .aliases" } # List all aliases from the aliases file function _list_aliases { cut -d' ' -f2- "$ALIASES_FILE" | command cat } # ============================================================================================================================================================== action="$1" param1="$2" param2="$3" # Check if the user has provided a help option or no arguments helpParams=('-h' '--help' '-help') if (($helpParams[(Ie)$action])) || test $# -eq 0; then # Display usage information echo "Usage: mal [OPTION]... [ALIAS_NAME] [ALIAS_COMMAND]..." echo "" echo "Create, delete, change, or execute aliases interactively." echo "" echo "Options:" echo " -h, --help display this help and exit" echo " -l list all defined aliases" echo " -e execute an alias interactively" echo " -d delete an alias interactively" echo " -dn NAME delete an alias by name" echo " -r rename an alias interactively" echo " -rn OLD NEW rename an existing alias" echo " -c change the command associated with an existing alias interactively" echo " -cc NAME COMMAND change the command associated with an existing alias by name" return 0 fi case "$action" in "-e") # Execute an alias interactively _execute_alias ;; "-l") # List all aliases _list_aliases ;; "-d") # Delete an alias interactively _interactive_delete ;; "-dn") # Delete an alias using a parameter _parameter_delete "$param1" ;; "-r") # rename an alias interactively _interactive_rename ;; "-rn") # rename an alias using parameters _parameter_rename "$param1" "$param2" ;; "-c") # change a command interactively _interactive_command_change ;; "-cc") # change a command using parameters _parameter_command_change "$param1" "$param2" ;; *) # add a new command _add_command "$action" "$param1" ;; esac # Reload aliases source "$ALIASES_FILE" }