2

I really like bash aliases, it annoys me that every time I want to add a new alias I have type two commands:

echo "alias \"short-cmd\"='long-command'" >> ~/.bash_aliases
source ~/.bash_aliases

Any way I can create an alias permanently with a single command?

2
  • 1
    You can use . instead of source and press [Alt]+[.] to insert the last argument of the previous command, so you can do the equivalent of source ~/.bash_aliases.sh in just four keystrokes without the need for any custom functions.
    – n.st
    Commented May 8, 2014 at 16:34
  • The point is do everything in a single command
    – RSFalcon7
    Commented May 8, 2014 at 17:14

3 Answers 3

3

Here is my workaround, the function palias (aka permanent alias):

function palias ()  {

    if [ $# -ne "2" ] ; then
        error "Usage: palias short-alias \'long-alias\'"
        return
    fi

    alias $1="$2"
    alias $1 >> ~/.bash_aliases
}
2
  • 1
    FYI: Running alias without a definition will show the current definition. You can use this to make sure the quoting works right—e.g., your current method fails if the definition contains single-quote. alias "$1" >> ~/.bash_aliases does not. I suggest switching (in both your answer and on your systems). Also, you shouldn't be using echo -e there; you want plain echo (if you don't want to switch to alias for some reason).
    – derobert
    Commented Mar 28, 2014 at 17:59
  • @derobert I solved some expansion problems and applied the alias trick you told me. Thanks a lot
    – RSFalcon7
    Commented May 8, 2014 at 17:38
0

My "two keystroke solution" (one letter and the return key) is to have this alias set up:

alias a='. ~/.bash_aliases'

Then whenever I update my .bash_aliases file I just type areturn

One step further is maintain them across machines, using github:

bup () { [ $# = 1 ] && { cp -v ~/$1 ~/Dropnot/setups; cd ~/Dropnot/setups; git fetch; git merge origin/master; git add $1; git commit -m"$1 update"; git push origin master; cp -v $1 ~; cd -; } || echo "Error - no filename passed!";}

Usage: bup [file] # file must be in ~, is usually a dot file]

-2

There are several files that run when you login to your shell including ~/.bash_profile and possibly ~/.bashrc and ~/.bash_aliases if those are referenced in your ~/.bash_profile file. To have an alias available every time you login, just put the command that creates the alias in one of those files.

http://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/

1
  • I know that! The point is to make it permanent without resourcing any file
    – RSFalcon7
    Commented Mar 28, 2014 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .