The Quick and Dirty on the Terminal and Text Editors

The TerminalOne of the most revolutionary, most useful, and most cool aspects of the Mac OS X operating system is the fact that it all runs on top of a core system based on UNIX. The average user never needs to know that, but if you want to go beyond the options Apple puts easily at your fingertips, and are willing to roll up your sleeves and deal with things like "mod_dav" and "awk", you can do all sorts of wonderful things.

The TerminalThe key to this wide and wonderful world is the Terminal, an application that lets you deal with UNIX directly, mano a mano (or mano a computadora, maybe. I don't speak Spanish.). The Terminal is not as user-friendly as the rest of OS X, though, and takes a little initiation. So, because I use the Terminal and things related to it often when I try to tell people how to do things on a Mac, I present this simple introduction to the Terminal and to editing files with it. If you want to learn more about UNIX, there will always be lots left to learn, and I know very little of it. So at the bottom of this page, I've listed a few links to get you started. If you don't want to deal with so much of the nitty-gritty, the basics covered here should take you a long way.

Choosing a Text Editor

Before you start anything, you need to choose a program to edit text in. There are a number of ways of going about this. The wrong way is to try to use Microsoft Word or Apple's TextEdit. These programs are for word processing purposes, and put all sorts of stuff into text files to keep track of things like alignment and text formatting, which is basically junk when it comes to doing the sorts of things we'll be doing. The most accessible option that comes built right in is called 'vi'. To open a file in vi, you would type something like vi filename into the Terminal. There are alternatives to vi which some people swear by, such as emacs and pico, but I don't know how to use either one. If you would like to use one of those, please check out their documentation online. Also, if you own the wonderful BBEdit program, this can be effectively used as an alternative to these command-line programs, and is very easy to use. But I will write the rest of this tutorial under the assumption that you will be using vi.

VimActually, to be honest, I don't use vi. I use its upgraded cousin, vim. They're basically the same, but vim lets you see things in color, and it has very useful features like unlimited undos, and I can't see any reason why you wouldn't use it instead of vi. So, because you can always go back to vi if you want to, I'd suggest downloading Marc Liyanage's vim package for OS X. It's very easy to install - just open the installer and follow the instructions. From this point on I will assume that you are using vim, but most everything is the same for vi.

Now you're almost set. The last setup thing I'd suggest is getting a nice vim config file from somewhere, because vim is very customizable, and this can make it even nicer. You can try out mine by opening a new Terminal window (you can find the Terminal application in /Applications/Utilities) and typing curl -O http://www.gregwestin.com/.vimrc. This downloads my .vimrc file to your home directory. There are definitely better ones out there, and I frankly just copied mine from various people who know better, but this one at least lets you do things like use the arrow keys when you're in insert mode, a feature without which vim is amazingly annoying. Now, you need to close your open Terminal window and open a new one, so that this new vim configuration file will take effect.

Learning to Write

Now, some vim basics. To open a file, you simply type vim filename and then hit return. To type, you need to type i (don't hit return afterwards) to enter insert mode, then type your text, then hit <esc> to leave insert mode. To save the file, type :w and then return, to quit use :q, or to do both use (:wq).

There are just a few more things that are helpful, especially when editing large files. None of the following commands can be issued in insert mode, so make sure you type <esc> to leave insert mode before using them. To search for something within the file, use the / command. For example, to search for "LoadModule", I would type /LoadModule and then hit return. When you want to include slashes in your search, you need to 'escape' them with backslashes. To search for 'Directory "/Library/WebServer/Documents', for example, I would use /Directory "\/Library\/WebServer\/Documents. Generally, however, you won't have to search for anything nearly that long; /Directory " would probably suffice. If you don't get the line you're looking for, you can repeat the last search simply by typing / and hitting return.

Sometimes, you won't know exactly what you're looking for. If you know the line number that you're looking for, just type it and then G. If you just want to move up or down quickly, use ctrl-b, ctrl-f, ctrl-u, or ctrl-d (full page backwards, full page forward, half page backwards, and half page forwards, respectively). To jump from word to word, use w.

The last few crucial skills in editing text are all related in vim - deleting, cutting, and pasting text. To delete the current line, type dd. To delete multiple lines, type d, then the number of lines (not including the current one) to delete, and then a direction to delete in (up or down). If you don't have vim set up to respond appropriately to your arrow keys, you'll have to use j (down) or k (up) instead of the arrow keys (j, k, h, and l can also be used to navigate through the document, but I don't know why anyone would prefer that to the arrow keys). When you delete text in this way, you're actually cutting it. You can paste it somewhere else by navigating to the line after which you want the text inserted, and typing p.

Those few skills should be all you need to edit files effectively using vim. If you're going to be editing a lot of text, I think it is definitely worth it to invest in something like BBEdit (I use BBEdit to create all the code for my web sites), but vim is perfect for quick edits to configuration files and the like, for reading README files that come with UNIX installs, etc. (I use vim for this sort of thing).

Happy hacking!


-Greg

UNIX links:

Home Resume Contact Photos Computers Links