4.5. Editing Text FilesWhile editing text files in the GUI is straightforward using the built-in TextEdit program (/Applications), editing files using the command-line tools hasn't always been a walk in the park, unless you are comfortable with old-school text editors like vi or Emacs. But thanks to the open command, several of the command-line editing tasks that you need to perform can be done in the GUI. For example, to open your bash history file in TextEdit, use the following command: $ open ~/.bash_history For most text-file editing, however, TextEdit is a little underpowered. Also, it can't be used as the command-line editor program (via the EDITOR environment variable), and it can't be used to edit files while you are logged in remotely.
4.5.1. nanoIf you do need to edit text files on the command linesay you are logged in remotely or need to edit a file owned by rootthe easiest text editor to use on the system is nano. To use nano to edit a file, use the following command: $ nano filename When you use nano, you'll see the user interface shown in Figure 4-5. In place of a toolbar, there is a series of commands at the bottom of the screen. The caret (^) character prefixing all those lines stands for the Control key on your keyboard. Simply press the Control key plus the letter of the command to perform some action. For example, to save a file, which nano refers to as "WriteOut," hit Control-O. To open, or "Read" a file, hit Control-R. And to exit, hit Control-X.
Aside from the Control key combinations, nano is fairly straightforward to use. Arrow keys move the cursor up and down, and whatever you type is inserted wherever the cursor is located. Figure 4-5. The nano text editor4.5.2. vi and EmacsMac OS X also ships with the two venerable Unix powerhouses of text editing: vi and Emacs. Each of these tools has a long history and feature sets that are even longer. These tools are powerful indeed and up to the task of any amount of text editing that you want to doincluding hardcore developmentbut in many ways, they are more complicated than what you need for everyday tasks. However, if you are already a user of either of these two editors, they are available for use on the system.
This isn't the place for a full tutorial on how to use vi or Emacs, but you should be able to exit either of these shells if you find yourself in one. To exit vi, type the following: :q! To exit Emacs, type Control-X, and then Control-C. |