To start vi:
Example: > vi letter will open a new file called letter to edit, or if letter already exits, open the exiting file.
|edit filename starting at line 1
|vi +n filename
|edit filename beginning at line n
|edit filename beginning at the last line
|vi -r filename
|recover filename after a system crash
|vi +/patter filename
|edit filename starting at the first line containing pattern
Command Mode vs. Insert Mode
Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to be in when giving commands which will move the cursor, delete text, copy and paste, save the file etc.
When entering a file, vi is in command mode. To enter text, you must enter insert mode. If in insert mode, enter command mode by hitting the escape, <esc>, key.
To insert text:
|at the end of the line
|open a line below the current line
|open a line above the current line
|replace the current character
|replace characters until <ESC>, overwrite
To move the cursor:
You must be in Command Mode to use commands that move the cursor. Each of these commands can be preceded with a Repeat Factor.
8j will move the cursor down 8 lines
3w will move the cursor 3 words to the right.
|Moves the cursor
|SPACE, l (el), or right arrow
|space to the right
|h or left arrow
|space to the left
|j or down arrow
|down one line
|k or up arrow
|up one line
|word to the right
|word to the left
|end of the line
|beginning of the line
|end of the word to the right
|beginning of previous line
|end of the sentence
|beginning of the sentence
|end of paragraph
|beginning of paragraph
To Delete Text:
The d command removes text from the Work Buffer. The amount removed depends on the Repeat Factor and the Unit of Measure you enter after d. If you delete by mistake: give the command u (undo) immediately after you give the delete command.
Examples: 3dd will delete 3 lines beginning with the current line.
3dw or d3w will delete 3 words
|delete to beginning of line
|delete to end of word
|delete to end of third word
|delete to beginning of word
|delete to end of blank delimited word
|delete to beginning of blank delimited word
|delete current line
|delete 5 lines starting with the current line
|delete through the last line on the screen
|delete through the first line on the screen
|delete through the end of the sentence
|delete through the beginning of the sentence
|delete the current character
|delete the number of characters specified by n.
|delete n characters before the current character
Viewing Different Parts of the Work Buffer:
^Character means that you should hold down the Control key while striking the indicated character key.
|Moves the cursor
|forward one-half screenful
|backward one-half screenful
|forward one screenful
|backward one screenful
|to line n (Ex: 25G moves the cursor to line #25)
|to the top of the screen
|to the middle of the screen
|to the bottom of the screen
|refresh the screen
Yanking (copy) and Putting (paste) Text:
Example: 3yy will yank (copy) 3 lines
p will put the 3 lines just yanked on the line below the current cursor.
In the following list M is a Unit of Measure that you can precede with a Repeat Factor, n.
|yank text specified by M
|yank 3 words
|yank n lines
|yank to the end of the line
|put text above current line
|put text below current line
Example: cw allows you to change a word. The word may be replaced by as many word as needed. Stop the change by hitting < esc >.
c3w allows you to change 3 words.
Ending an Editing Session
|writes the contents of the work buffer to the file
|quit without saving changes
|save and quit
|save and quit
|saves to filename (allows you to change the name of the file)
|join the current line and the following line
|number the lines on the screen (not actually added to file)
|turns off numbering of lines
|reads filename into the current file at the location of the cursor
|displays INPUT MODE at the lower right hand corner of screen
|change uppercase to lowercase and vice-versa
Searching for an expression
When in command mode if your enter / the cursor will go to the bottom of the screen. Follow this with an expression and vi will find the next occurrence of that expression in the file. Example: /Bill will find the next occurrence of Bill. If this is followed by the command: n, the next occurrence of Bill is found.