Tuesday, October 4, 2011

UNIX VI editor

The vi editor is a command-line editor that has 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.

Inserting and Appending Text

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

Moving the Cursor Within the vi Editor

h,left arrow,or Backspace - Left one character
j or down arrow           - Down one line
k or up arrow             - Up one linel,
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
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                 - 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

Editing Files by Using the vi Editing Commands

x                          - Deletes a character at the cursor.
dw                         - Deletes a word
dd                         - Deletes the line
:n,nd                       - Deletes Lines n –n  

Using the Text-Replacing Commands

/string                    - Searches forward for the string.
?string                    - Searches backward for the string.
                         - Searches for the next occurrence.
N        - Searches for the previous occurrence of the string.

Using the Text-Copying and Text-Pasting Commands

yy :- Yanks a copy of a line
:- Puts yanked or deleted text under the line containing
the cursor.
P  :- Puts yanked or deleted text before the line containing
the cursor

Using the File Save and Quit Commands

: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

Summery

Search Functions                                               Move and Insert Text 
/exp Go forward to exp                                      :3,8d Delete line 3-8
?exp Go backward to exp                                  :4,9m 12 Move lines 4-9 to 12
                                                                           :2,5t 13 Copy lines 2-5 to 13
 Save Files and Exit                                           :5,9w file Write lines 5-9 to file
:w Write to disk
:w newfile Write to newfile                                Screen/Line Movement
:w! file Write absolutely                                         Move cursor down
:wq Write and quit                                              k     Move cursor up
:q Quit editor                                                      h     Move cursor left
:q! Quit and discard                                            l     Move cursor right
                                                                            0     Go to line start (zero)
Word Movement                                                  $     Go to line end                   
w     Go forward 1 word                                    G     Go to last file line
b     Go backward 1 word


Delete Text                                                          Add/Append Text
    Delete 1 character                                         a     Append after cursor
dw     Delete 1 word                                             A     Append at line end
dd     Delete 1 line                                                 i     Insert before cursor
D     Delete to end of line                                      5i     Insert text 5 times
d0     Delete to start of line                                   I     Insert at beginning of line
dG     Delete to end of file

Copy and Insert Text                                            Add New Lines
    Yank a copy                                                   o     Open a line below cursor
5Y   Yank a copy of 5 lines                                   O     Open a line above cursor
p     Put below cursor
P     Put above cursor

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes