Creating Shortcuts With User Aliases

By | 2007/03/30

Do you have a set of commands that you find you repeat over and over in your daily use? Things like connecting to servers over ssh, rsyncing data launching apps with a slew of argumets. You know what I mean. Well you can make things easier on yourself by creating alias commands for your account. Below are a few examples:

ssh user@server -p 22222 could be setup in an alias defined as ‘connectwork’

rsync -avz –progress daily/backup/file/ user@server:/remote/directory could be defined as ‘dailybackup’

By setting these longer commands as local aliases all you have to do is type the alias name, which is then connected to the command and you’ve saved yourself a load of typing. Yes, this appeals to us because we work hard but don’t want to work any harder than needed!

To create an alias you’ll want to edit your local .bashrc file. This file defines a number of shell options for your account but also can store any aliases you want to define. The default file has a few example aliases set, and all you have to do to create your own are append them to the list.  A sample entry would look like this:

alias aoeu=’setxkbmap us’ (this will switch keyboard layouts to qwerty)

alias asdf=’setxkxmap dvorak’ (this will switch keyboard layouts to dvorak)

alias updateme=’sudo aptitude update && sudo aptitude upgrade && sudo aptitude dist-upgrade && sudo aptitude autoclean’ (check for, download, install and cleanup after updates)

As you can tell from these few examples you’re able to create an alias for your user on the shell for whatever you like.  It can save you typing, help automate repetitive commands, and make your life easier.  Give it a try.

12 thoughts on “Creating Shortcuts With User Aliases

  1. mpt

    I’d use shell aliases more if there was a GUI for managing them…

  2. Marius Gedminas

    I like creating one-line shell scripts in ~/bin instead of putting aliases in .bashrc. It’s easier to manage them (e.g. if I decide that this one would be useful on another machine, I can scp ~/bin/foo machine:bin/ which is easier than opening an editor.

    Also, when you edit your .bashrc you need to source it in all the terminals you already have open, or your new aliases will only work in new terminal windows. Shell scripts start working immediately.

  3. Marius Gedminas

    Another thing: instead of writing (or aliasing) ssh -p 22222 user@server you can create a ~/.ssh/config file with

    Host work
    User user
    HostName server
    Port 2222

    and then you can use ssh work or scp somefile work:somedirectory without repeating usernames, hostnames or port numbers.

  4. Sak

    Awesome tip, aliases save a lot of time. But a more proper place to put (and manage them) would be in something like a ~/.bash_aliases file. In ~/.bashrc, find the section that looks like this

    # Alias definitions.
    # You may want to put all your additions into a separate file like
    # ~/.bash_aliases, instead of adding them here directly.
    # See /usr/share/doc/bash-doc/examples in the bash-doc package.

    #if [ -f ~/.bash_aliases ]; then
    # . ~/.bash_aliases
    #fi

    And uncomment those last 3 lines. That way you have 1 file just for your own custom aliases and they can be more easily managed and separate from the bash config file.

  5. mycroft

    None of this is working on my install. I changed the /etc/profile call, but still it does not call my .~/.bash_aliases script.

    Any other ideas?

    I see aliases are being created somewhere, but I can’t find ’em. Help!!!

    Thanks

  6. mycroft

    Well, my apologies to anyone listening…

    Being the newbie I am, I didn’t realize that my Gnome-terminal application would allow me to set it so that is process the command shell correctly.

    I use the Korn shell because I need to mimic usage on an HPUX system, and that is the default shell used on that server. I want to use my laptop as a developer workstation, and figured Ubuntu, running ksh would work for me. So, far so good. The fix is easy, as you just need to go to the gnome-terminal profile being used, click on the Edit Profile option, click the ‘Title and Command’ tab, and enable the checkbox for ‘Run a command as a login shell’. Close the session, and re-open, and your ~/.profile will be processed. So, you can do as you like setting aliases or variable in that file for personal use, or create or call another file to set them up.

    Sometimes the easiest thing to do is the hardest to see, but this is working for me, and I can use ksh, too, without a lot of fuss and bother.

    Cheers

  7. Review Man

    And how do I enable ~/.profile and aliases herein to get read by gnome-terminal?

  8. sobaken

    Thank you Sak!

    tip about .bash_aliases it what i’ve looked for! 🙂

  9. MJWiMac

    PLEASE HELP!! I created an alias in .bashrc by nano .bashrc then i entered alias iMac=ssh\ -p\ XXXXX\ First\ [email protected].X then i saved it and ran source /.bashrc But when I type in a clean shell “iMac”… I get this error “ssh: Could not resolve hostname First: nodename nor servname provided, or not known.”…. NOW, I have tried the alias without the “\” every where els but the name, But the closest I get is above, if I just put a space inbetween my first and last name (my username) it just tells me it stops at “Michael”… i tried using _ and / and putting them before and after the space… ITS DRIVING MY NUTZ!!! my email is [email protected] please help!!!

  10. MJWiMac

    CORRECTION MY PERVIOUS POST!!
    I FIGURED IT OUT!!! if you have a space in your user name and you are creating an alias to connect to it using ssh, in nano .bashrc. Type alias iMac(or watever)=ssh\ -p\ XXXXX\ “First\ Last”@XX.XXX.XXX.X NOTICE THE ” ” around my first and last name, THIS IS REQUIRED… NOT JUS THE \ before the space!!! I DONT KNOW WHY, but there ya go!

  11. Neil

    did not work in ubuntu 10.10
    I had to put all in .bashrc

Comments are closed.