Overview
<esc> if in doubt
i insert
a append
:q! quit no save
d<space> delete a character
D delete to end of line
r replace
dd delete a line
u undo
25yy copy (yank) 25 lines
p paste / put
:set nu numbers the lines
More detail
The cursor is moved using either the arrow keys or the h,j,k,l keys.
h (left) j (down) k (up) l (right)
To enter Vi (from the % prompt) type: % vi FILENAME <RETURN>
To exit Vi type: <ESC> :q! <RETURN>
OR type: <ESC> :wq <RETURN> to save the changes.
To delete a character under the cursor in Command Mode type: x
To insert text at the cursor while in Command Mode type:
i type in text <ESC>
NOTE: Pressing <ESC> will place you in Command Mode
or will cancel an unwanted and partially completed command.
To delete from the cursor to the end of a word type: dw
To delete from the cursor to the end of a line type: d$
To delete a whole line type: dd
The format for a command in command mode is:
[number] command object OR command [number] object
where:
number - is how many times to repeat the command
command - is what to do, such as d for delete
object - is what the command should act upon, such as w (word),
$ (to the end of line), etc.
To undo previous actions, type: u (lowercase u)
To undo all the changes on a line type: U (capital U)
To undo the undo's type: CTRL-R
=====>>>>> THE PUT COMMAND <<<<<=====
** Type p to put the last deletion after the cursor. **
Move the cursor to the first line in the set below.
Type dd to delete the line and store it in Vi's buffer.
Move the cursor to the line ABOVE where the deleted line should go.
While in Command Mode, type p to replace the line.
=====>>>>> THE REPLACE COMMAND <<<<<=====
** Type r and a character to replace the character under the cursor. **
Move the cursor to the first line below marked --->.
Move the cursor so that it is on top of the first error.
Type r and then the character which should replace the error.
Repeat steps 2 and 3 until the first line is correct.
=====>>>>> THE CHANGE COMMAND <<<<<=====
** To change part or all of a word, type cw . **
Move the cursor to the first line below marked --->.
Place the cursor on the u in lubw.
Type cw and the correct word (in this case, type 'ine'.)
Press <ESC> and move to the next error (the first character to be changed.)
Notice that cw not only replaces the word, but also places you in insert.
=====>>>>> MORE CHANGES USING c <<<<<=====
** The change command is used with the same objects as delete. **
The change command works in the same way as delete. The format is:
[number] c object OR c [number] object
The objects are also the same, such as w (word), $ (end of line), etc.
Move to the first line below marked --->.
Move the cursor to the first error.
Type c$ to make the rest of the line like the second and press <ESC>.
To replace text that has already been deleted, type p . This Puts the
deleted text AFTER the cursor (if a line was deleted it will go on the
line below the cursor).
To replace the character under the cursor, type r and then the
character which will replace the original.
The change command allows you to change the specified object from the
cursor to the end of the object. eg. Type cw to change from the
cursor to the end of the word, c$ to change to the end of a line.
The format for change is:
[number] c object OR c [number] object
=====>>>>> LOCATION AND FILE STATUS <<<<<=====
** Type CTRL-g to show your location in the file and the file status.
Type SHIFT-G to move to a line in the file. **
Note: Read this entire lesson before executing any of the steps!!
Hold down the Ctrl key and press g . A status line will appear at the
bottom of the page with the filename and the line you are on. Remember
the line number for Step 3.
Press shift-G to move you to the bottom of the file.
Type in the number of the line you were on and then shift-G. This will
return you to the line you were on when you first pressed Ctrl-g.
(When you type in the numbers, they will NOT be displayed on the screen.)
4. If you feel confident to do this, execute steps 1 through 3.
=====>>>>> THE SEARCH COMMAND <<<<<=====
** Type / followed by a phrase to search for the phrase. **
In command mode type the / character. Notice that it and the cursor
appear at the bottom of the screen as with the : command.
Now type 'errroor' <RETURN>. This is the word you want to search for.
To search for the same phrase again, simply type n .
To search for the same phrase in the opposite direction, type Shift-N .
If you want to search for a phrase in the backwards direction, use the
command ? instead of /.
---> When the search reaches the end of the file it will continue at the start.
=====>>>>> MATCHING PARENTHESES SEARCH <<<<<=====
** Type % to find a matching ),], or } . **
Place the cursor on any (, [, or { in the line below marked --->.
Now type the % character.
The cursor should be on the matching parenthesis or bracket.
Type % to move the cursor back to the first bracket (by matching).
---> This ( is a test line with ('s, ['s ] and {'s } in it. ))
Note: This is very useful in debugging a program with unmatched parentheses!
=====>>>>> A WAY TO CHANGE ERRORS <<<<<=====
** Type :s/old/new/g to substitute 'new' for 'old'. **
To change every occurrence of a character string between two lines,
type :#,#s/old/new/g where #,# are the numbers of the two lines.
Type :%s/old/new/g to change every occurrence in the whole file.
Ctrl-g displays your location in the file and the file status.
Shift-G moves to the end of the file. A line number followed
by Shift-G moves to that line number.
Typing / followed by a phrase searches FORWARD for the phrase.
Typing ? followed by a phrase searches BACKWARD for the phrase.
After a search type n to find the next occurrence in the same direction
or Shift-N to search in the opposite direction.
Typing % while the cursor is on a (,),[,],{, or } locates its
matching pair.
To substitute new for the first old on a line type :s/old/new
To substitute new for all 'old's on a line type :s/old/new/g
To substitute phrases between two line #'s type :#,#s/old/new/g
To substitute all occurrences in the file type :%s/old/new/g
To ask for confirmation each time add 'c' :%s/old/new/gc
Some useful examples are:
:!dir - shows a directory listing of your area.
:!delete FILENAME - removes file FILENAME from your area.
:w FILENAME writes the current Vi file to disk with name FILENAME.
:#,# FILENAME saves the lines # through # in file FILENAME.
:r FILENAME retrieves disk file FILENAME and inserts it into the
current file following the cursor position.
Typing o opens a line BELOW the cursor and places the cursor on the open
line in insert mode.
Typing a capital O opens the line ABOVE the line the cursor is on.
Type an a to insert text AFTER the character the cursor is on.
Typing a capital A automatically appends text to the end of the line.
Typing a capital R enters replace mode until <ESC> is pressed to exit.
Typing ":set xxx" sets the environment variable "xxx"
0 comments on vi notes