8 Comments

Summary:

Today I wanted to give a little tip for making regular arguments in the Terminal, a little easier to access. OS X offers the ability to create aliases of anything in the Finder – files, folders, applications – for quick access outside their typical location. Aliases […]

unix tip Today I wanted to give a little tip for making regular arguments in the Terminal, a little easier to access. OS X offers the ability to create aliases of anything in the Finder – files, folders, applications – for quick access outside their typical location. Aliases in a Command Line Interface are very similar, allowing you to create a name for a command or collection of arguments for easier execution.

We’ll store our list of alias commands in the .profile file which resides in your home directory. Notice the period in front of the file name? (.profile) That . signifies that the file is hidden from the Finder, so you probably haven’t seen it in your Home folder before. (There are tools that allow you to make invisible files show up in the Finder – Cocktail is just one of many.) So how do we edit a file we can’t see in the Finder? Well since we’re talking Unix tips, we’ll be doing it all in the CLI, so let’s get started.

We’ll be editing our .profile in a text editor that’s built into the Unix system, and it’s called the VI editor. It’s a little different to get used to it, but with a little practice you should be able to get the hang of using it. Fire up the Terminal and make sure you’re starting in your Home directory. So to create your .profile type:
vi .profile
You should now be looking at a blank terminal window with nothing more than tilde’s down the left edge of the window.
To begin creating the file contents, you need to enter a typing mode, to do this, hit the ‘i’ key (for insert). Now that we can enter text, try a couple of my favorites as aliases to try to get used to things:
alias ll=’ls -al’
alias ls=’ls -l’
alias ..=’cd ..’

I’ll break this down first, then tell you how to save them:
We start by saying this line will be an alias. Next is the name of the alias we’re creating. Then an equals statement that defines the argument you want the alias to execute. The aliases I just showed you are as follows:
ll (or ls -al) : a list command that shows me all files in a directory, including hidden files
ls (changed to ls -l) : ls is the default list command, but I’ve mapped it to add the -l, which shows me additional information about the visible files in the current directory. (info like permissions, dates, sizes)
.. (or cd ..) : a quick alias for moving back up in a file path

So how do we save these now that we’ve typed them in?
First we need to exit the insert mode. To do this hit the ESC key. Now you’ve left the mode of editing the file. If you want to save your changes, type:
:wq [return]
If you wish to move on without saving the file:
:q! [return]
Both of these commands will exit you completely from the VI editor and back to the directory structure of the file system that you were viewing before entering the editor’s functions.

Now just quit your Terminal app, reopen it, and your aliases should now be functional and available for use. As you come up with longer and more complicated arguments to run in the CLI, using aliases will aid in saving you a great deal of time.

Now a couple of statements to cover myself…

* I don’t claim to be an expert on VI. I’m absolutely certain there are many out there – possibly reading this – that know boat loads more than I do. I’m just scratching the surface in order to give people a taste of VI, which I may cover in more detail later. For now, this is just a little taste to get people interested.

* The .profile is not necessarily the typical choice for storing aliases, but if I’m not mistaken, it’s recognized across various shells, so is the easiest to address for the masses.

  1. I spend about 12 hours a day in the Linux CLI environment, but at the end of a hard day I like to curl up with my PowerBook… without the terminal open!

    However, for those pesky little tasks I still like to use the CLI for my PowerBook. Navigation at the console level, however, is not friendly and you can end up cd’ing your way into oblivion. So, I use two aliases called “here” and “there” which facilitate getting around the directory tree.

    Say you are in /Users/user/Desktop/folder/subfolder/ (which I will refer to as Dir1) and you want to edit a file in /Volumes/thumbdrive/folder/subfolder/ (which I will refer to as Dir2). That’s a lot of typing to get back and forth, no? So, while in Dir1 you simply type “here” at the CLI. When you are done messing around in Dir2 and you want to go back to Dir1, all you have to do is type “there” to go back. Out of the 30 aliases that I have, these are my favorite.

    alias here ‘setenv THERE_PWD `echo $PWD`’
    alias there ‘cd $THERE_PWD’

    -Bret

    Share
  2. Thanks Bret!!!
    That was a really good one

    Share
  3. Martin Stanhope Tuesday, January 23, 2007

    Nice tip. These days one is likely to be using a bash shell on OS X which would make it:

    alias here=’export THERE_PWD=`echo $PWD`’
    alias there=‘cd $THERE_PWD’

    Martin

    Share
  4. Good call Martin. Forgot to mention that the syntax was for csh.

    Share
  5. I’ve created an alias which will quickly move to a “sibling” directory. Say we have two folders: ~/Desktop/Ned and ~/Desktop/Fred
    If your currenty working directory is ~/Desktop/Ned and u want to switch your current working directory to Fred, normally you’d type:

    cd ../Fred

    Too much typing for me! Create an alias like this:

    alias c,=”cd ..;cd ”

    Now you can go from Ned to Fred (or visa versa) like this:

    c, Fred

    This saves a few keystrokes!

    Share
  6. I am a newbie to unix environment and mac osx as well, very nice tip and i hope you people contribute more here so that people like me can learn:)

    Regards
    Mutahir

    Share
  7. Re: here and there,

    there are already built-in commands which do a similar thing for you: pushd and popd, but they don’t stick around like your ‘here’ does.

    pushd /some/long/new/path (to go to the new place and remember the current one)
    popd (to return to the remembered one)

    Share
  8. My favorite alias is the good ol’ long list but with color and better file size info. Of course to get the color benefit you have to add that setting (in Terminal prefs on Mac OS X or in your profile: export TERM=xterm-color).

    alias lsh="ls -alhG"

    I also have aliases for all the common SVN commands that I use all day long, like:

    alias sva="svn add"
    alias svc="svn commit"
    alias svd="svn delete"
    alias svi="svn info"
    alias svm="svn move"
    alias svs="svn status"
    alias svu="svn update"

    Share

Comments have been disabled for this post