Three basic modes of operation :
- command mode
- edit mode
- last line mode
Command Mode
The command mode is the default mode for the vi editor. In this mode, you can perform commands to delete, change, copy, and move text. You can also position the cursor, search for text strings, and exit the vi editor.
Edit Mode
You can enter text into a file in the edit mode. The vi editor interprets everything you type in the edit mode as text. To enter the edit mode, perform the commands:
i - Inserts text before the cursor
o - Opens a new blank line below the cursor
a - Appends text after the cursor
Last Line Mode
You can use advanced editing commands in the last line mode. To access the last line mode, enter a colon (:) while in the command mode. The colon places your cursor at the bottom line of the screen.
Switching Between the Command and Edit Modes
The default mode for the vi editor is the command mode. When you perform an i, o, or a command, the vi editor switches to the edit mode. After editing a file, press Escape to return the vi editor to the command mode. When in the command mode, you can save the file and quit the vi editor.
The following example shows how to switch modes in the vi editor:
1. Perform the vi filename command to create a file. You are automatically in the command mode.
2. Type the i command to insert text. The i command switches the vi editor to the edit mode.
3. Press Escape to return to the command mode.
4. Perform the :wq command to save the file and exit the vi editor.
TABLE OF CONTENTS
1. Using the VI editor
2. Inserting and appending text
3. Moving cursor within VI editor
4. Editing files by using VI editing commands
5. Using text changing commands
6. Using the text replacing commands
7. Using the text copying and text pasting commands
8. Using the file save and quit commands
9. Customizing the VI session
Using the vi editor
vi filenamevi options filenameTo recover a file :
vi -r filenameTo open file in read-only mode :
vi -R filenameor
view filenameInserting and appending text
Command | Function |
|---|---|
a | Appends text after the cursor |
A | Appends text at the end of the line |
i | Inserts text before the cursor |
I | Inserts text at the beginning of the line |
o | Opens a new line below the cursor |
O | Opens a new line above the cursor |
:r filename | Inserts text from another file into the current file |
Key Sequence | Cursor Movement |
|---|---|
h, left arrow, or Backspace | Left one character |
j or down arrow | Down one line |
k or up arrow | Up one line |
l, right arrow, or space bar | Right (forward) one character |
w | Forward one word |
b | Back one word |
e | To the end of the current word |
$ | To the end of the line |
0 (zero) | To the beginning of the line |
^ | To the first non-white space character on the line |
Return | Down to the beginning of the next line |
G | Goes to the last line of the file |
1G | Goes to the first line of the file |
:n | Goes to Line n |
nG | Goes to Line n |
Control-F | Pages forward one screen |
Control-D | Scrolls down one-half screen |
Control-B | Pages back one screen |
Control-U | Scrolls up one-half screen |
Control-L | Refreshes the screen |
Control-G | Displays current buffer information |
Command | Function |
|---|---|
R | Overwrites or replaces characters on the line at and to the right of the cursor. To terminate this operation, press Escape. |
C | Changes or overwrites characters from cursor to the end of the line. |
s | Substitutes a string for a character at the cursor. |
x | Deletes a character at the cursor. |
dw | Deletes a word or part of the word to the right of the cursor. |
dd | Deletes the line containing the cursor. |
D | Deletes the line from the cursor to the right end of the line. |
:n,nd | Deletes Lines n -n (for example, :5,10d deletes Lines 5-10). |
Command | Function |
|---|---|
cw | Changes or overwrites characters at the cursor location to the end of that word |
r | Replaces the character at the cursor with one other character |
J | Joins the current line and the line below |
xp | Transposes the character at the cursor and the character to the right of the cursor |
~ | Changes the case of the letter, either uppercase or lowercase, at the cursor |
u | Undoes the previous command |
U | Undoes all changes to the current line |
. | Repeats the previous command |
Command | Function |
|---|---|
cw | Changes or overwrites characters at the cursor location to the end of that word |
r | Replaces the character at the cursor with one other character |
J | Joins the current line and the line below |
xp | Transposes the character at the cursor and the character to the right of the cursor |
~ | Changes the case of the letter, either uppercase or lowercase, at the cursor |
u | Undoes the previous command |
U | Undoes all changes to the current line |
. | Repeats the previous command |
Command | Function |
|---|---|
yy | Yanks a copy of a line |
p | Puts yanked or deleted text under the line containing the cursor |
P | Puts yanked or deleted text before the line containing the cursor |
Command | Function |
|---|---|
:n,n co n | Copies Lines n -n and puts them after Line n. For example, :1,3 co 5 copies Lines 1-3 and puts them after Line 5. |
:n,n m n | Moves Lines n -n to Line n. For example, :4,6 m 8 moves Lines 4-6 to Line 8. Line 6 becomes Line 8. Line 5 becomes Line 7. Line 4 becomes Line 6. |
Command | Function |
|---|---|
:w | Saves the file with changes by writing to the disk |
:w new_filename | Writes the contents of the buffer to new_filename |
:wq | Saves the file with changes and quits the vi editor |
:x | Saves the file with changes and quits the vi editor |
ZZ | Saves the file with changes and quits the vi editor |
:q! | Quits without saving changes |
Command | Function |
|---|---|
:set nu | Shows line numbers |
:set nonu | Hides line numbers |
:set ic | Instructs searches to ignore case |
:set noic | Instructs searches to be case sensitive |
:set list | Displays invisible characters, such as ^I for a Tab and $ for end-of-line characters |
:set nolist | Turns off the display of invisible characters |
:set showmode | Displays the current mode of operation |
:set noshowmode | Turns off the mode of operation display |
:set | Displays all the vi variables that are set |
:set all | Displays all set vi variables and their current values |
1. Create a file in your home directory named .exrc.
2. Enter the set variables into the .exrc file.
3. Enter set variable without the preceding colon.
4. Perform one command on one line.
The vi editor reads the .exrc file located in your home directory each time you open a vi session, regardless of your current working directory.

Brak komentarzy:
Prześlij komentarz