by http://webgeektutorials.blogspot.com

Friday, September 9, 2011

VI Editor : helpful command reference


VI is text editors which share certain characteristics, such as methods of invocation from the operating system command interpreter, and characteristic user interface features. 
vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or normal mode (where keystrokes are interpreted as commands that control the edit session). For example, typing i while in normal mode switches the editor to insert mode, but typing i again at this point places an "i" character in the document. From insert mode, pressing the escape key switches the editor back to normal mode.

vi Modes
vi has two modes:
  1. command mode
  2. insert mode
In command mode, the letters of the keyboard perform editing functions (like moving the cursor, deleting text, etc.). To enter command mode, press the escape <Esc> key.
In insert mode, the letters you type form words and sentences. Unlike many word processors, vi starts up in command mode. In order to begin entering text in this empty file, you must change from command mode to insert mode. To do this, type i .

Some helpful commands in Insert mode

i at the same cursor position
Capital A
To the end of the line.
Capital I
Begin of the line.
Small a
Next to the cursor position.

yy

Copy single line.

5yy
for five lines copy
nyy
n number of lines can copy.
1G
Goes to first line
10G
Goes to tenth line
11G
Goes to eleventh line
nG
Goes to the nth position
1H
Goes up to the current screen
Capital G
Goes to the end of the page
yw
To copy a word
nyw
n number of words can be copied
x
One character delete
dw
One word delete
dd
Delete a line
ndd
n number of lines can be deleted To replace a character
  • Type small 'r' and then type character.
  • To replace many characters from the cursor position Type capital R

Command Summary

STARTING vi
     vi filename    edit a file named "filename"
     vi newfile     create a new file named "newfile"
ENTERING TEXT
      i            insert text left of cursor
     a            append text right of cursor
MOVING THE CURSOR
     h            left one space
     j            down one line
     k            up one line
     l            right one space
BASIC EDITING
x         delete character
     nx        delete n characters
     X         delete character before cursor
     dw        delete word
     ndw       delete n words
     dd        delete line
     ndd       delete n lines
     D         delete characters from cursor to end of line
     r         replace character under cursor
     cw        replace a word
     ncw       replace n words
     C         change text from cursor to end of line
     o         insert blank line below cursor
                  (ready for insertion)
     O         insert blank line above cursor
                  (ready for insertion)
     J         join succeeding line to current cursor line
     nJ        join n succeeding lines to current cursor line
     u         undo last change
     U         restore current line
MOVING AROUND IN A FILE
     w            forward word by word
     b            backward word by word
     $            to end of line
     0 (zero)     to beginning of line
     H            to top line of screen
     M            to middle line of screen
     L            to last line of screen
     G            to last line of file
     1G           to first line of file
     <Control>f   scroll forward one screen
     <Control>b   scroll backward one screen
     <Control>d   scroll down one-half screen
     <Control>u   scroll up one-half screen
     n            repeat last search in same direction
     N            repeat last search in opposite direction
CLOSING AND SAVING A FILE
      ZZ            save file and then quit
     :w            save file
     :q!            discard changes and quit file

No comments:

Post a Comment