4

This question already has an answer here:

Is there a command line util/script that automatically saves aliases? E.g.

save-alias my-ip="curl ipecho.net/plain"

and that will define an alias, then add/update it in ~/.bashrc or ~/.zshrc

I was thinking about writing a script that would define an alias and then automatically save it. Having to manually add an alias each time to the config file is annoying. But I thought perhaps I'm going to reinvent the wheel and that you guys already have some solution to this annoyance.

EDIT: I ended up writing the script which you can find in the answers.

marked as duplicate by Gilles, slm Sep 5 '14 at 23:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You've got this all wrong - you don't add aliases w/ echo - use alias. alias "$aliasname" >>rcfile. That's all you need. – mikeserv Jun 10 '15 at 20:33
  • @mikeserv I don't get how that works :(. My goal is to define and save aliases with one command. alias c >> ".aliases" seem to save only those aliases which are already defined. If I understand correctly I'll have to do alias c=cat; alias c >> .aliases? Right now I can just do save-alias c=cat, which both saves an alias to the file (while replacing any duplicates). What am I missing here? – Don Manley Jun 11 '15 at 11:19
2

To add it and also have it available immediately:

# In your .bashrc or .bash_profile file, add:
newalias() {echo "alias ${1}" >> $HOME/.bash_aliases; source ~/.bash_aliases; }

This implies using a .bash_aliases file and having it sourced from .bashrc or .bash_profile, with the line

. ~/.bash_aliases

Some folks might not have the .bash_aliases file initially and just have their aliases currently defined in their .bashrc or .bash_profile. This will still work because >> will create the .bash_aliases file if it doesn't currently exist, otherwise it will append to it. However you may then need to do the manual step of adding the reference to it and may wish to consider moving existing aliases to it at that point.

  • Copy and paste :) – Willian Paixao Sep 5 '14 at 15:23
  • @WillianPaixao not quite. The source command in this answer makes the alias immediately available, which your's does not. That's not an earth-shattering difference, but is is better than "logout and login again". – msw Sep 5 '14 at 22:32
  • I ended up writing another version, because your required double quotes and it appended duplicates, if you want you can check my answer. I used the "source" idea from your answer though. – Don Manley Sep 5 '14 at 22:38
1

That's actually a nice idea. I would create function:

addalias() {
    echo "alias ${1}" >> $HOME/.bash_aliases
}

Add this to my .bashrc, logout and login again.

The usage is:

addalias ..='cd ../../'
0

I ended up writing this script. I explain why in the end.

It adds new aliases to the .aliases file and loads them immediately. It does not append duplicates twice, so if you define the same alias more than once it will update previous versions, instead of spamming your config file with duplicates.

ALIASES_FILE_PATH=$HOME/.aliases

function save-alias() {

    ALIAS_NAME=`echo "$1" | grep -o ".*="`

    # Deleting dublicate aliases
    sed -i "/alias $ALIAS_NAME/d" $ALIASES_FILE_PATH

    # Quoting command: my-alias=command -> my-alias="command"
    QUOTED=`echo "$1"\" | sed "s/$ALIAS_NAME/$ALIAS_NAME\"/g"`

    echo "alias $QUOTED" >> $ALIASES_FILE_PATH

    # Loading aliases
    source $ALIASES_FILE_PATH
}

Another version

Instead of using a separate file for aliases it stores them in the config file itself (.zshrc or .bashrc). It also appends aliases only to a specified place in the config file so that you can keep other stuff below aliases, if you like. It will append the aliases before "# END ALIASES", so make sure that you have that exact string. So, e.g., your config file will look like this:

plugins=(git)
#and bla bla bla

# ALIASES
alias test-alias="echo I was added automatically"
# END ALIASES

# Yes you can have the bottom of the config file free.
# Because it will store aliases inside ALIASES block 
source $ZSH/oh-my-zsh.sh
# and etc

Which is accomplished by the following code:

CONFIG_PATH=$HOME/.zshrc

function save-alias() {

    ALIAS_NAME=`echo "$1" | grep -o ".*="`


      # Checking whether the alias name is empty. 
      # Otherwise sed command later will match and delete every alias in the file
    if [[ -z "$ALIAS_NAME" ]]; then
      echo 'USAGE: save-alias alias_name="command" ' 1>&2
      echo '       save-alias hello="echo hello world" \n' 1>&2
      echo "Wrong format. Exiting..." 1>&2
      exit 1
    fi

    # Deleting dublicate aliases
    sed -i "/alias $ALIAS_NAME/d" $CONFIG_PATH

    # Quoting command: my-alias=command -> my-alias="command"
    QUOTED=`echo "$1"\" | sed "s/$ALIAS_NAME/$ALIAS_NAME\"/g"`

    # Appending the command to the config (before "# END ALIASES")
    sed -i "/# END ALIASES/i alias $QUOTED" $CONFIG_PATH

    #reloading config file.
    source $CONFIG_PATH
    # instead of reloading the whole config you might want to append
    # to a new file as well, then source it and then rm new file
}

Why not other solutions presented here?

echo "alias ${1}" >> $HOME/.bash_aliases

There are two problems with them.

1. Quotes are removed

With the command above (represented as "save-alias") the following will fail

save-alias test-alias="echo hello world"

It will fail because quotes will be removed and it will append the command as this

alias test-alias=echo hello world

and it will fail with the following errors:

bash: alias: hello: not found
bash: alias: world: not found

The solution is to use double quotes:

save-alias test-alias='"echo hello world"'

which is a bit annoying

2. They append duplicates.

If you run a command twice you'll discover that the same alias will be added twice to the config file:

alias test-alias="echo hello world"
alias test-alias="oh no the config file is getting spammed" 

Not the answer you're looking for? Browse other questions tagged or ask your own question.