Permanent Alias in Mac Terminal
Posted on October 2, 2009
Anyone attempting to use the Terminal in MacOS X quickly realizes that doing so requires cryptic codes and long strings to accomplish simple tasks. One solution, for repetitive tasks, is to create aliases. An alias allows you to type a shortcut that the Terminal converts into the full set of tokens. For example, going up one directory in the tree requires the user to type, “cd ..”. You can make an alias that allows you to use “..” without the “cd ” and does the same thing. This can be done temporarily by typing alias ..=’cd ..’ at the command line. Doing so causes the alias to be created for the current session. But once you close the terminal, the alias is gone.
To make permanent aliases in the Mac OS X Terminal (shell) you will first need to VI (a Linux text editor) a file named .profile in your home directory. To do so, open Terminal (which goes to your home directory by default) and type:
vi .profile
Doing so opens .profile in the the VI editor. To start typing in the VI editor, press the “i” key (i as in eye). Then type the following:
alias la='ls -la'
Save the .profile file by pressing:
Esc and then Shift ZZ
Quit and re-run the Terminal. Once running type “la” at the command line and you should get a directory listing just as you would have had you typed “ls -la”.
Once you have proven that this is working for you, you can start adding other aliases to the .profile file. Here are some I like:
alias cls='clear' #old MS DOS
alias ..='cd ..' #up one level
alias ...='cd ../..' #up two levels
alias zap='rm -i' #remove file with warning
You can push new aliases into .profile without opening the file in VI. To do this, type the following at the command prompt:
echo alias e=\'exit\'>>.profile
# don't forget the \ characters before the single quotes.
# The \ escapes the quotes so that they are pushed into the file.
You don’t have to include the comments (i.e. #up one level).
Linux uses this same process but the aliases are stored in ~/.bashrc
Regular Expressions
Posted on July 11, 2007
TextPad® is a powerful, general purpose editor for plain text files. I have used it for years and recommend it highly. One of the best features is that you can do ‘find and replace’ requests using regular expressions.
I found a good set of regular expressions for finding phone numbers in text files. In the interest of saving you from reinventing the wheel, here they are.
The “^” character means that it will find only phone numbers that begin at the first character of the line. If you don’t want that, remove the “^”.
For numbers formatted like ###-###-#### or ###.###.####
^[0-9]\{3\}[-.][0-9]\{3\}[-.][0-9]\{4\}
For numbers formatted like (###)###-#### or (###)###.####
^[(][0-9]\{3\}[)][0-9]\{3\}[-.][0-9]\{4\}
For numbers formatted like (###) ###-####
^[(][0-9]\{3\}[)][[:space:]][0-9]\{3\}[-.][0-9]\{4\}
PsPad is another great text editor. It has an FTP feature that allows you to work on files on a server in real-time. The regular expressions for PsPad are as follows:
For numbers formatted like ###-###-#### or ###.###.####
^[0-9]{3}[-.][0-9]{3}[-.][0-9]{4}
For numbers formatted like (###)###-#### or (###)###.####
^[\(][0-9]{3}[\)][0-9]{3}[-.][0-9]{4}
For what it is worth, TextPad is a better utility if you are trying to clean up text files. PsPad is better if you are working on files that are on a web server. I use PsPad for development and TextPad for things like regular expression finds.
Filed Under Tips | Leave a Comment