Sie sind auf Seite 1von 20

VI Editor

Originally developed by William Joy in the late 1970s, vi


(pronounced "vee-eye") is a visual text editor.

vi modes
vi has two different modes. One is Command Mode, the other is Text Entry Mode:
Command Mode
In Command Mode everything that you type is interpreted as a command. For
most commands, a single keystroke is the entire command, while other
commands require more information, such as a linecount followed by a singlecharacter command. Command mode is used to perform powerful editing
functions, as well as such actions as writing the text buffer to a file. To enter
Command Mode from Text Entry mode, type an <ESC> character.
Text Entry Mode
In Text Entry mode all characters typed are printed on the screen and become
part of the current document. Editing in this mode is restricted to insertion and
typeover. To enter Text Entry Mode from Command Mode, type the character 'i'
(for insert mode), 'a' (for append mode), or 'o' (for open mode).

Description
vi is actaully the command which starts the visual mode
of ex, the landmark editing program developed by Joy. As
ex gained popularity, Joy noticed that most users were
exclusively using its visual mode, so to make things more
convenient for his users, he added a link to ex which
started it in visual mode automatically. Today vi is the
most popular text editor among Linux users.
A more feature-rich implementation of vi named vim
(which stands for "vi improved") is also available.

Options

Using vi
vi is an interactive text editor which is display-oriented:
the screen of your terminal acts as a window into the file
you are editing. Changes you make to the file are
reflected in what you see.
Using vi you can insert text anywhere in the file very
easily. Most of the vi commands move the cursor around
in the file. You can move the cursor forward and
backward in units of characters, words, sentences, and
paragraphs. Some of the operators, like d for delete and
c for change, can be combined with the motion

Editing A File

The most common way to start a vi session is to tell it


which file to edit. To edit a file named filename, use the
command:
vi filename
The screen will clear and the text of your file will appear
on the screen. If filename doesn't exist yet, vi will start
you in a new file, and when you tell it to save your work,
it will use the filename that you specified.

The Editor's Copy: The "Buffer"


The editor does not directly modify the file you are
editing. Instead, it makes a copy of this file in memory
called the buffer. You do not actually affect the contents
of the file until you write the changes you've made back
into the original file.

Arrow Keys

Finding Out Where You Are In


The File
You can find out where you're at in the file by pressing
^G. This sill show you the name of the file you are
editing, the number of the current line, the number of
lines in the buffer, and the percentage of the way
through the buffer your cursor is currently at.

Moving Within A Line


w will advance the cursor to the next word on the line, and b
will back up the cursor to the previous word.
e advances you to the end of the current word rather than the
beginning of the next word.
If words are punctuated, for instance with an apostrophe or a
comma, w and b will stop at the punctuation. If you use W and
B instead, they will move the cursor while ignoring punctuation.
These commands all wrap at the end (or beginning) of a line,
moving to the previous or next line if their search for the next
or previous word goes that far.

Writing, Quitting, and Editing New


Files
So far we have seen how to write our file to disk and quit (ZZ), or simply write our file
and continue editing(:w).
If you have changed your file but you want to quit without saving, use the command :q!.
The command :q quits the editor, but it will only let you do this if you haven't made any
changes since your last write (your last save). The exclamation point, however, tells the
editor "yes, I really want to do this."
Similarly, you can edit another file with the :e name command, where name is the name
of the file you want to edit. But the editor won't let you do this if you have unsaved
changes. However, if you use the exclamation mark again (:e! name), the editor will do
as you ask: it will discard your changes and open file name for editing.
If you don't specify a filename, and just use the command :e!, the editor will re-load your
saved version of the file you're working on. This is like the "revert" command in other file
editors. It will discard your changes, and revert to the last-saved version of the file.

Search & Replace


vi also has powerful search and replace capabilities. To search the text of an open file for
a specific string (combination of characters or words), in the command mode type a
colon (:), "s," forward slash (/) and the search string itself. What you type will appear on
the bottom line of the display screen. Finally, press ENTER, and the matching area of the
text will be highlighted, if it exists. If the matching string is on an area of text that is not
currently displayed on the screen, the text will scroll to show that area.
The formal syntax for searching is:
:s/string
For example, suppose you want to search some text for the string "cherry." Type the
following and press ENTER:
:s/cherry
The first match for "cherry" in your text will then be highlighted. To see if there are
additional occurrences of the same string in the text, type n, and the highlight will switch
to the next match, if one exists.

The syntax for replacing one string with another string in the current line is
:s/pattern/replace/
Here "pattern" represents the old string and "replace" represents the new string. For
example, to replace each occurrence of the word "lemon" in a line with "orange," type:
:s/lemon/orange/
The syntax for replacing every occurrence of a string in the entire text is similar. The
only difference is the addition of a "%" in front of the "s":
:%s/pattern/replace/
Thus repeating the previous example for the entire text instead of just for a single line
would be:
:%s/lemon/orange/

Working with multiple Files


It is easy to insert text into an open file from another file. All that is
necessary is to move the cursor to the location where you want the text
inserted, then type
:r filename
where "filename" is the name of the file to insert.
For example, if you want to copy the contents of the file "peach" into the
file "fruit," you would first position the cursor to the desired line in "fruit"
and then type
:r peach
Notice that this operation causes no change to the file "peach."

You can also append text from the currently open file to any other file. This is accomplished
using the :w (colon + "w") command followed without a space by >>. For example, to
append the contents of a currently open file named "pear" to the file named "apple," type
:w>> apple
At times it can be convenient to open multiple files simultaneously. This is efficiently
accomplished by just listing all of the files to be opened after the vi command. For
example, to simultaneously open files about three kinds of fruit, type:
vi apple pear orange
This allows you to edit "apple" first. After saving "apple," typing :n calls up "pear" for
editing.
If you want to simultaneously open all files in the current directory, just type vi * (vi +
space + asterisk).

Inserting
r
replace character under cursor with next
character typed
R
i

keep replacing character until [esc] is hit


insert before cursor

append after cursor

append at end of line

O
mode

open line above cursor and enter append

Deleting
x

delete character under cursor

dd

delete line under cursor

dw

delete word under cursor

db

delete word before cursor

Das könnte Ihnen auch gefallen