Выбрать главу

4.4.1.5. Searching

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 .

4.4.1.6. Undoing, redoing, and repeating

Pressing u will undo the last operation performed; pressing Ctrl-R will redo it. Typing a dot ( . ) will repeat the last operation.

4.4.1.7. Saving and exiting

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

Command Description
:w Write (save) using the current filename.
:w newfilename Write to the file newfilename (subsequent :w commands will still write to the original filename).
:w! Force-write (write even if in read-only mode).
:q Quit (succeeds only if the document is saved).
:q! Force quit even if the document isn't saved (abort!).
:wq or :x or ZZ Write and quit (exit with save).

4.4.2. How Does It Work?

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 terminali.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 screento 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.

4.4.3. What About...

4.4.3.1. ...using vi with a GUI?

If you execute gvim instead of vi , a window will appear with a full graphical user interfaceincluding pull-down menus and a toolbaras 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.

Figure 4-5. gvim: vi with a GUI

4.4.3.2. ...using other text editors?

In addition to vi , Fedora ships with a plethora of other text editors, including:

nano (an improved clone of the easy-to-use editor Pico )

mcedit

joe (the commands jstar , jmacs , or jpico will start joe configured to emulate WordStar , emacs , or Pico ).

emacs and emacs-x

kedit and gedit

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.