Introduction Vi(Visual Editor) in Linux Operating System
Vi(Visual Editor)
Entering Input Mode
vi starts up
in command mode. To enter text, you need to switch to input mode. vi provides several ways to do this. For example, the command a puts vi in input mode and begins appending typed text into the buffer immediately after the position of the cursor. (Note that it appends text
after the character
pointed to by the cursor, not after the line pointed to, as in ed.) The
command i puts vi in input mode and begins inserting typed text immediately before the position of the cursor. The command A puts vi in input
mode and appends material at the end of the
current line. The command I puts
vi in input mode and inserts material at the beginning of the current line. The command O (uppercase O) Opens
up a line above the current line,
places the cursor there, and puts vi in
input mode. The command o (lowercase o) opens
up a line below the current line, places
the cursor there, and puts vi in input mode.
All further typing that you do is entered into the buffer. Whenever you are in input mode, existing text moves as new text is entered. The new text you type in does not overwrite the old.
Leaving Input Mode
Because vi is a two-mode editor, the most important commands for a beginner to remember are the ones that are needed to change modes. The
commands a and A, i and I, and o and O place you in input mode.
When you are done creating text, you can leave input mode
and go into command mode by pressing ESC (the escape key is usually at the
upper-left of the keyboard). Anytime you press ESC, even in the middle of a line, you will be put back in command mode. The only way to stop appending or inserting text and return to command mode is to press ESC. This gets vi out
of input mode and back into command mode.
To automatically keep track of where you are (command or input mode), it is a good idea to press ESC as soon as you are done entering a portion of text. This puts you back into command mode.
Exiting vi
When you have finished typing
in
your text, you
need to exit the editor.
Remember, if you
make
serious errors, you can always exit and start again. First get out of input mode by hitting
ESC. Typing : (colon) puts
you
in a mode in which ed commands work. The cursor drops
to
the bottom of the screen,
prints a : (colon), and waits. The command : w will write the contents of the editing buffer into
the file. It is at this point that the original file is replaced with the new, edited version. The combined
commands : wq will write
and quit. Since : wq is
a common command sequence in every editing
session, the abbreviation ZZ, which represents “last command,” is equivalent to : wq. The command : x stands for exit
and is also equivalent to : wq. If
you have made some changes you regret,
you can cancel all the changes you’ve made by quitting the editor without
writing the buffer to a file. To do this, use the : q! command.
This means “quit, and I really mean it.”
Moving Within a Window
The main benefit of a
screen editor is that you can see a portion of your file and use context to move around and decide on changes. In vi’s command mode, you have several ways to move around a window. One set of commands enables you to move around by lines or characters|
l or SPACEBAR or → |
Moves right one character |
|
h or CTRL-H or BACKSPACE or ← |
Moves left one character |
|
j or CTRL-J or CTRL-N or ↓ |
Moves down one line |
|
k or
CTRL-P or ↑ |
Moves
up one line |
|
0 |
Moves to the beginning of the line |
|
$ |
Moves to the end of the current line |
|
+ or ENTER |
Moves
to the beginning of the next line |
|
− |
Moves
to the beginning of the previous line |
You don’t need to think only in terms of characters and lines; vi also lets you move in other units. In normal text entry, a word is a sequence of letters delimited by spaces or by punctuation; and a sentence ends with a period (.), question mark (?), or exclamation point (!) and is separated from the previous sentence by two spaces, or by a ENTER. With these definitions, the commands shown in below
|
Command |
What It Does |
|
w |
Moves
to the next word or punctuation mark |
|
W |
Moves
to the next word |
|
e |
Moves to the end of this word or punctuation mark |
|
E |
Moves
to the next end of word |
|
b |
Moves back to the beginning of word or punctuation |
|
B |
Moves back to the beginning of the word |
|
) |
Moves to the start of the next sentence |
|
( |
Moves back to the start of the sentence |
|
} |
Moves
to the start of the next paragraph |
|
{ |
Moves back to the last start of paragraph |
|
]] |
Moves to the start of the next section |
|
[[ |
Moves back to the last start of section |
Moving the Window in the Buffer
|
CTRL-F
(Forward) |
Moves forward one full screen |
|
CTRL-D (Down) |
Moves forward one half screen |
|
CTRL-B (Back) |
Moves back one full screen |
|
CTRL-U (Up) |
Moves back one half screen |
|
G (Go) |
Moves to end of file |
These commands also take numeric prefixes to move further ahead in the file. The command 3CTRL- F (hitting the number 3 key, then the CTRL and F keys simultaneously) will move ahead three full screens, whereas the command 4CTRL-B will move back four screens. The G command goes to a specific line number or goes to the end if no line number is specified. Therefore, the command 23G positions the cursor at line 23; the
command 1G (number 1 and letter G) positions it at the first line in the file, whereas
the command G goes to the last line.
Modifying Text
vi provides simple commands for changing and deleting parts of your text. The command rn means replace the current character (where the
cursor is located) with the character n. You
can also replace multiple characters; for example, the command 3rn replaces three characters with n.
The command RstringESC
replaces the current characters with the string
you type in. Characters are overwritten until you press ESC (the escape key, usually in the upper left-hand of the keyboard).
The c (change) command enables you to make
larger-scale modifications to words or lines. For example, the command cwstringESC
changes the current word
by replacing it (that is, overwriting it) with whatever string you type. The change continues until you press ESC. When you make such a change, vi puts a $ over the last character of the word to be changed. The $ disappears when you press ESC.
The command c$stringESC
will change everything from the current cursor position to the
end of the line ($) by replacing the text with the string you type in. ESC takes you out of input
mode.
The change commands also can take numerical arguments, so the command 4cw will change
the next four words, and the command
3c$ will change
the next three lines.
DeletingText
vi provides two delete commands that let you delete small or large chunks of text. To delete single letters, use the command x. x deletes the current character. As with other vi commands, x takes a numerical argument. This means that the command 7x deletes seven characters-the character under the cursor and the six to the right of it. The d (delete) command works on larger units of text.
|
Command |
What It Does |
|
dw |
Deletes from the cursor to the end of the word |
|
3dw |
Deletes three words |
|
d$ |
Deletes to the end of the line |
|
D |
Deletes to the end of the line (a synonym for d$) |
|
3d$ |
Deletes to the end of the third line ahead |
|
d) |
Deletes to the beginning of the next line |
|
d} |
Deletes
to the beginning of the next paragraph |
|
d]] |
Deletes to the beginning of the next section |
|
dd |
Deletes the current line |
|
2dd |
Deletes two lines |
Comments
Post a Comment