Unix Tip – 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.

loading

Comments have been disabled for this post