Tuesday, August 5, 2008

Using The Vi Editor

The vi Text Editor.

The vi text editor can be used to edit ASCII text files while you are working in a shell window. It is a standard editor found on most unix systems. To open or create a file in vi, from your command line type:

vi filename

If you are creating a new file, filename is what you want to call the file. If you are editing an existing file, type the current filename. The file appears in your shell window.

Unlike most word processors you may be familiar with, vi has two modes of operation that you must toggle back and forth between in order to edit your files. One mode is for inserting text, and the other mode is for moving around or executing commands. The two modes are called:

Text Input mode. (also called insert mode) Command mode (also called escape mode) When you first enter vi, you are in command mode, and cannot enter text until you change modes. You can switch to Text input mode by hitting the i key (for insert). After hitting the i key, any characters you type will be inserted at the current cursor position. Alternatively you could enter insert mode by pushing the a key (for append), and characters you type will be appended to the current cursor position. To get back to escape mode you push the escape key. You need to be in Command mode before you can quit out of a file.


Short summary of vi Commands.

Use the following commands in Command mode to cut, paste, and move around in the text. Use them also to switch from Command mode to Text Input mode. Remember to hit Esc whenever you want to switch from Text Input mode to Command mode.


Adding Text in vi

Use these Command mode keystrokes to switch to Text Input mode (hit or to return to Command mode):

a Add text after the cursor
A Add text to the end of the current line
i Insert text at the cursor
I Insert text at the beginning 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

Use these Command mode keystrokes to cut and paste text:

x Delete the letter beneath the cursor
dw Delete the letter beneath the cursor and the rest of the word
# dw Delete the following number of words, including the current word
D Delete from the cursor to the end of the line
dd Delete the current line
# dd Delete the following number of lines, including the current line
yy Copy or "yank" the current line
# yy Copy or "yank" the following number of lines, including the current line
p Paste the current copied or deleted letters, words, or lines after the cursor
J Join the next line with the current line (erases a carriage return)
u Undo the last edit
. Redo the last editing command


Moving Around in a vi Text File

Use these Command mode keystrokes to move around within the file:
j Move down to the next line
k Move up to the previous line
h Move backward by letter
l Move forward by letter
w Move forward by word
b Move backward by word
e Move forward to the end of a word
CTRL F Move forward to next screen
CTRL B Move backward to previous screen
CTRL D Move forward one-half of a screen
G Move to the end of the file
# G Move to the specified line number in the file
$ Move to the end of the current line
0 Move to the beginning of the current line
/ wordSearch for specified word
n Search for next occurrence of specified word
N Search for previous occurrence of specified word


Saving or Quitting vi

Use these Command mode keystrokes to save or quit out of a file:
:wq Save and quit out of the file
:w Save the current file without quitting
:q Quit if no edits have occurred
:q! Quit without saving edits

No comments: