You are not logged in.

#1 2012-10-27 15:44:06

From: Cambridge, UK
Registered: 2011-11-01
Posts: 2

A bit of fun: Alias+

I use the console a lot. To remedy the tedium of remembering and typing long commands, 'alias' as an excellent tool. Unfortunately, and for good reason, shortcuts defined with 'alias' are not user-persistent. The usual solution is for users to maintain their own set of aliases in their .bashrc configuration. I think there's a better way.

Alias+ is a (very) simple bash script (two functions, in fact), that lives inside your .bashrc config, and maintains such a list for you.

== Key Features ==

- It's absolutely tiny, and is pure bash
- It's not fussy: It can live happily with whatever else you put in your .bashrc
- It's clean: It keeps track of its own entries with a tag at the end of each line

== Usage ==
There are three commands:
  alias+, with no arguments, prints a usage guide and a list of active aliases
  alias+, with two arguments, adds a new alias
  alias-, with one argument, deletes the named alias

The first command also reloads aliases from the file. This is useful when you have more than one terminal open, and would like to propagate a new entry to other terminals.

== Installation ==

Step 1) Copy the alias+ script into your .bashrc file: (It ain't pretty, but it's compact)

alias+() { 
    if [ $# -eq 2 ]; then 
        echo "alias $1=\"$2\" #alias+" >> ~/.bashrc;
        source ~/.bashrc;
        source ~/.bashrc; 
        echo ""; echo "alias+: list shortcuts"; echo "alias+ x y: add shortcut"; echo "alias- x: delete a shortcut"; echo ""; 
        grep "^alias " ~/.bashrc | grep "#alias+$" | sed "s/^alias /\x1B[1m/g;s/=\"/\x1B[0m => /g;s/\" #alias+$//g"; echo ""; 
alias-() { 
    (cat ~/.bashrc | grep -v "^alias $1=") > ~/.bashrc.NEW; 
    mv ~/.bashrc.NEW ~/.bashrc; 
    unalias $1;

alias untar="tar -xf" #alias+

Step 2) Load the new config into any open terminals:

$ source ~/.bashrc

Step 3) Test it by running:

$ alias+

You should see a usage guide, and the included 'untar' alias.

You can give alias+ control of any existing aliases by adding ' #alias+' to the end of their respective lines.

Last edited by ryansuchocki (2012-10-28 15:54:20)


#2 2012-10-27 16:04:45

Registered: 2010-05-06
Posts: 5,080

Re: A bit of fun: Alias+

This is nice! I have two suggestions:
1. How about leaving the .bashrc alone, writing the aliases to a seperate file and sourcing that file?
2. Instead of "cat $FILE | grep $EXPRESSION" use "grep $EXPRESSION $FILE"


#3 2012-10-27 16:31:29

Registered: 2011-07-05
Posts: 71

Re: A bit of fun: Alias+

Interesting idea, here's my version for zsh, which sources a file only for aliases:

alias_mod() {
  case "${#@}" in
    1) sed -i "/alias\s${@[1]}=/d" "${myaliases}" ; unalias "${@[1]}" ;;
    2) echo "alias ${@[1]}='${@[2]}'" >> "${myaliases}" ; source "${myaliases}";;
    *) echo "invalid number of arguments" ;;

Single command, action depends on number of arguments.

Last edited by avx (2012-10-27 17:54:20)


#4 2012-10-28 14:50:37

From: Pittsburgh
Registered: 2009-04-25
Posts: 1,172

Re: A bit of fun: Alias+

Nice.  I'll now use a variation of this as a compliment to my 'realias' bash function (keeping my user's aliases in a separate file).

realias ()
    unalias -a
    $EDITOR ~/.bash_aliases
    . ~/.bash_aliases

Aside: @ryansuchocki, it is easier to read scripts and code if you enclose the code segments with the BBCode tags '[⁣code]' and '[/⁣code]' – … s_and_Code.  You are able to edit your original post to add the tags.


#5 2012-10-28 16:16:50

From: Cambridge, UK
Registered: 2011-11-01
Posts: 2

Re: A bit of fun: Alias+

Thanks, people big_smile. Useful feedback all round.


Board footer

Powered by FluxBB