Fedora Core, like most other Linux and Unix systems, stores most of its configuration information in text files. These files can be edited using various system administration tools, but they can also be edited by hand using any standard text editor.
vi is one such text editor. Some people love it, and some people hate it, but it has one advantage over just about every other editor available: it’s universal. If you know how to use vi, you can confidently walk up to just about any Linux or Unix computer in the world and edit text files, so it’s a valuable skill. The other nice fact about Vi is that it’s not very demanding; you can use it in character mode or graphic mode, over a congested remote connection or with a foreign keyboard, and still get the job done. You can get by with less than a dozen commands to start, and then learn more when you need them.
Tip
vi is pronounced “vee-eye,” not “vye” or “six.”
To start up the vi editor, simply type its name at a shell prompt, optionally providing the name of a file you wish to edit as an argument:
$vi
filename
The screen will clear, and the specified file will be displayed, as shown in Figure 4-4.
Notice that unused lines are marked with a tilde (~) character.
vi uses two distinct modes:
The lower-left corner of the display shows the current mode: if it says --
INSERT
--
, then you’re in insert mode; otherwise, you’re in normal mode.
You can move the cursor around using the arrow keys. If your arrow keys don’t work (which may be the case if you’re using a remote connection from a bad terminal program), you can use the h, j, k, and l keys, as shown in Table 4-7.
Table 4-7. Basic vi movement commands
Command | Description |
---|---|
Left, h, or Backspace | Move left one character. |
Down or j | Move down one line. |
Up or k | Move up one line. |
Right, l, or Space | Move right one character. |
Enter | Move to the start of the next line. |
Home, ^, |, or 0 (Zero) | Move to the start of the line. |
End, $ | Move to the end of the line. |
:number Enter | Move to line number . |
:0 Enter | Move to the start of the file. |
:$ | Move to the end of the file. |
w | Move forward one word. |
You can put a number in front of any command to repeat the command. For example, typing 10j
will move down 10 lines.
There are several commands for inserting text, as shown in Table 4-8.
All of these commands place the editor into insert mode; the only difference is where the cursor is positioned for the inserted text. The word --
INSERT
--
will appear in the lower-left corner of the display.
To exit from insert mode and return to normal mode, press Esc. The --
INSERT
--
indicator in the lower-left corner of the display will disappear.
vi offers three basic commands for deleting or yanking, as shown in Figure 4-9. Deleting is roughly equivalent to cutting in a GUI-based editor, and yanking is similar to copying.
Table 4-9. Basic delete and yank commands
Typing /
followed by some text (actually, a regular expression) and pressing Enter will search forward in the document. Typing n
will repeat the previous search in the forward direction. Typing ?
instead of /
will search backward instead of forward; N
will repeat a search backward.
Searching can be combined with deleting and yanking; for example, d/hello
will delete from the cursor position up to the start of the word hello
.
Pressing u will undo the last operation performed; pressing Ctrl-R will redo it. Typing a
dot (.
) will repeat the last operation.
There are a number of commands available for saving the document and exiting vi, as shown in Table 4-10; you must press Enter after these commands.
Table 4-10. Saving text and exiting vi
vi is one of a group of programs that uses a terminal-control system called curses. curses enables an application to manage a character-mode display by positioning the cursor and interpreting keystrokes using a database of information about each terminal—i.e., which codes to send to produce different effects and which codes can be received from the terminal. Fedora’s terminfo database has entries for about 2,500 different hardware terminals and terminal programs that have been produced through the years.
curses keeps two buffers—areas of memory arranged in the same size and layout as the screen—to store the current terminal screen contents and the desired display. When vi needs to update the screen, it updates the display buffer; curses compares the two buffers to determine the minimum number of commands it can send to the terminal to produce the desired display. It then sends the appropriate codes from the terminal database (terminfo/termcap) to update the terminal, copies the display buffer to the terminal buffer, and repeats the process.
The version of vi used by Fedora Core is Vim (Vi iMproved), which adds many, many features to the traditional vi capabilities; the commands covered in this chapter outline only the basics. Vim offers syntax highlighting, macro recording, support for multiple character sets, accents, right-to-left text, and many other features, making it a useful text editor for programming, scripting, and editing system files.
Vim can be configured by creating a
.vimrc file; for details, type :help
vimrc-intro
within Vim.
If you execute
gvim instead of vi, a window will appear with a full graphical user interface—including pull-down menus and a toolbar—as shown in Figure 4-5. Using the File→Save menu option, clicking on the Save toolbar icon, or typing the vi save command (
:w
) will perform the same operation.
In addition to vi, Fedora ships with a plethora of other text editors, including:
All of these text editors are capable of editing just about any text file. Each has its advantages and disadvantages.
Since the choice of editor is very personal, take some time to experiment with each of the editors to see which one you prefer. In any case, I’d recommend knowing the basics of vi so that you can always fall back to it if you encounter a situation where your favorite editor is unavailable.
The Vim web site: http://www.vim.org/
The vi help file and online tutorial: start vi, then type
:help
and press Enter
Get Fedora Linux now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.