Fedora 6 and Red Hat Enterprise Linux Bible

It's almost impossible to use Linux for any period of time and not need to use a text editor. If you are using a GUI, you can run gedit , which is fairly intuitive for editing text. Most Linux shell users will use either the vi or emacs commands to edit text files. The advantage of vi or emacs over a graphical editor is that you can use it from any shell, a character terminal, or a character-based connection over a network (using telnet or ssh , for example). No GUI is required.

This section provides a brief tutorial of the vi text editor. The tutorial was done using the vi or vim (Vi Improved) editors provided with the vim-minimal and vim-advanced packages in Fedora Core.

Any time in this book that I suggest you manually edit a configuration file, you can use vi to do that editing (from any shell). (If vi doesn't suit you, see the "Exploring Other Text Editors" sidebar for other options.)

Exploring Other Text Editors

There are dozens of text editors available to use with Linux. Here are a few contained in Fedora and RHEL that you can try out if you find vi to be too taxing:

If you use ssh to log in to other Linux computers on your network, you can use any editor to edit files. A GUI-based editor will pop up on your screen. When no GUI is available, you will need a text editor that runs in the shell, such as vi, jed , or joe .

The vi editor is difficult to learn at first. But when you know it, you will be able to edit and move around quickly and efficiently within files. Your fingers never have to leave the keyboard to pick up a mouse or press a function key.

Starting with vi

Most often, you start vi to open a particular file. For example, to open a file called /tmp/test , type the following command:

$ vi /tmp/test

If this is a new file, you should see something similar to the following:

~ ~ ~ ~ ~ "/tmp/test" [New File]

The box at the top represents where your cursor is. The bottom line keeps you informed about what is going on with your editing (here you just opened a new file). In between, there are tildes ( ~ ) as filler because there is no text in the file yet. Now here's the intimidating part: there are no hints, menus , or icons to tell you what to do. On top of that, you can't just start typing. If you do, the computer is likely to beep at you. And some people complain that Linux isn't friendly.

The first things you need to know are the different operating modes. The vi editor operates in either command mode or input mode. When you start vi , you are in command mode. Before you can add or change text in the file, you have to type a command to tell vi what you want to do. A command consists of one or two letters and an optional number. To get into input mode, you need to type an input command. To start out, type either of the following input commands:

Type a few words and press Enter. Repeat that a few times until you have a few lines of text. When you are done typing, press Esc. You are now back in command mode. Now that you have a file with some text in it, try moving around in your text with the following keys or letters.

Tip 

Remember the Esc key! It always places you back into command mode.

Now that you know how to input text and move around, the only other editing you need to know is how to delete text. Here are a few vi commands for deleting text:

If you feel pretty good about creating text and moving around the file, you may want to wrap things up. Use the following keystrokes for saving and quitting the file:

Tip 

If you've really trashed the file by mistake, the :q! command is the best way to exit and abandon your changes. The file reverts to the most recently changed version. So, if you just did a :w , you are stuck with the changes up to that point. If you just want to undo a few bad edits, press u to back out of changes.

You have learned a few vi editing commands. I describe more commands in the following sections. However, before I do, here are a few tips to smooth out your first trials with vi :

Moving Around the File

Besides the few movement commands described earlier, there are other ways of moving around a vi file. To try these out, open a large file that you can't do much damage to. (Try copying /var/log/messages to /tmp and opening it in vi .) Here are some movement commands you can use:

Searching for Text

To search for the next occurrence of text in the file, use either the slash ( / ) or the question mark ( ? ) character. Within the search, you can also use metacharacters. Here are some examples:

The vi editor was originally based on the ex editor. That editor did not let you work in full-screen mode. However, it did enable you to run commands that let you find and change text on one or more lines at a time. When you type a colon and the cursor goes to the bottom of the screen, you are essentially in ex mode. Here is an example of some of those ex commands for searching for and changing text. (I chose the words Local and Remote to search for, but you can use any appropriate words.)

Using Numbers with Commands

You can precede most vi commands with numbers to have the command repeated that number of times. This is a handy way to deal with several lines, words, or characters at a time. Here are some examples:

Putting a number in front of most commands just repeats those commands. At this point, you should be fairly proficient at using the vi command. If you would like further instruction, I suggest you try the VIM Tutor by running the vimtutor command.

Note 

When you invoke vi on Fedora or RHEL, you're actually invoking the vim text editor, which runs in vi compatibility mode. Those who do a lot of programming might prefer vim , because it shows different levels of code in different colors. vim has other useful features, such as the ability to open a document with the cursor at the same place where it was when you last exited that file.

Категории