Chapter 5. Editing Text

To configure your system, you need to edit text files. Most of them will be in the /etc directory; and you'll need to su to root to be able to change them. You can use the easy ee, but in the long run the text editor vi is worth learning. There's an excellent tutorial on vi in /usr/src/contrib/nvi/docs/tutorial if you have that installed; otherwise you can get it by ftp to in the directory FreeBSD/FreeBSD-current/src/contrib/nvi/docs/tutorial.

Before you edit a file, you should probably back it up. Suppose you want to edit /etc/rc.conf. You could just use cd /etc to get to the /etc directory and do:

    # cp rc.conf rc.conf.orig
This would copy rc.conf to rc.conf.orig, and you could later copy rc.conf.orig to rc.conf to recover the original. But even better would be moving (renaming) and then copying back:
    # mv rc.conf rc.conf.orig
    # cp rc.conf.orig rc.conf
because the mv command preserves the original date and owner of the file. You can now edit rc.conf. If you want the original back, you'd then mv rc.conf rc.conf.myedit (assuming you want to preserve your edited version) and then
    # mv rc.conf.orig rc.conf
to put things back the way they were.

To edit a file, type

    # vi filename
Move through the text with the arrow keys. Esc (the escape key) puts vi in command mode. Here are some commands:

delete letter the cursor is on


delete the entire line (even if it wraps on the screen)


insert text at the cursor


insert text after the cursor

Once you type i or a, you can enter text. Esc puts you back in command mode where you can type

to write your changes to disk and continue editing


to write and quit


to quit without saving changes


to move the cursor to text; /Enter (the enter key) to find the next instance of text.


to go to the end of the file


to go to line n in the file, where n is a number


to redraw the screen

Ctrl-b and Ctrl-f

go back and forward a screen, as they do with more and view.

Practice with vi in your home directory by creating a new file with vi filename and adding and deleting text, saving the file, and calling it up again. vi delivers some surprises because it's really quite complex, and sometimes you'll inadvertently issue a command that will do something you don't expect. (Some people actually like vi—it's more powerful than DOS EDIT—find out about the :r command.) Use Esc one or more times to be sure you're in command mode and proceed from there when it gives you trouble, save often with :w, and use :q! to get out and start over (from your last :w) when you need to.

Now you can cd to /etc, su to root, use vi to edit the file /etc/group, and add a user to wheel so the user has root privileges. Just add a comma and the user's login name to the end of the first line in the file, press Esc, and use :wq to write the file to disk and quit. Instantly effective. (You didn't put a space after the comma, did you?)