Wednesday, January 5, 2011

Editing files with the vi editor : An Introduction

The vi program is a text editor that you can use to edit any text and particularly programs.The following are some of the important keystroke commands to get around in vi.

Adding Text in vi
i       Notifies vi to insert text before the cursor
I       Insert text at the beginning of the current line
a       Notifies vi to append text after the cursor
A       Add text to the end of the current line.
o       Open a new line below the current line and add text
O       Open a new line above the current line and add text
s       Substitute the letter underneath the cursor with letter you type, and insert text
S or c   Delete the current line and substitute it with text you type
R or C   Replace current text with text you type

Cutting and Pasting Text in vi
D        Delete from the cursor to the end of the line
dd      Notifies vi to delete the current line
#dd    Delete the following number of lines, including the current line (where # is a number)
yy      yank (i.e. copy) one line into a general buffer
#yy   Copy or "yank" the following number of lines, including the current line (where # is a number)
x        Notifies vi to delete the current character
dw      Delete the letter beneath the cursor and the rest of the word
#dw    Delete the following number of words, including the current word (where # is a number)
p        Paste data that was cut with x or dd commands or copy with yy command
J        Join the next line with the current line (erases a carriage return)
u        Notifies vi to undo the last command
      Redo the last editing command

Moving Around in a vi Text File
Ctrl+f   Scroll up one page
Ctrl+b    Scroll down one page
/string  Search forward for string

Saving or Quitting vi
:q     Quit editor
:q!   Quit editor without saving changes
:w    Save the current file without quitting
:wq    Save changes and exit editor


:f    Display filename and current line number
Esc  Notifies vi to end the insert or append mode

No comments:

Post a Comment