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:
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> or <CTRL 3> whenever you want to switch from Text Input mode to Command mode.
Use these Command mode keystrokes to switch to Text Input mode (hit <Esc> or <CTRL 3> 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. |
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. |
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. |
Use these Command mode keystrokes to save or quit out of a file:
:wq or ZZ | 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. |