1.
Table of Contents |
---|
Starting vi and dabbling with it
Code Block | ||
---|---|---|
| ||
vi i # get into input mode :a # add text after the cursor |
In input mode, type anything you want.2.
Saving in vi:
Esc |
---|
Get out of input mode and into command mode
...
Code Block | ||
---|---|---|
| ||
:w |
Enter |
---|
3.
Exiting vi:
Esc |
---|
Code Block | ||
---|---|---|
| ||
:q # quit vi |
...
- We can use : wq to save and quit vi.
- If you want to save a file over an existing file, use :w! existingfilename in command mode. The ! forces vi to overwrite the original.
...
Adding and deleting text
vi
i
type some you want to add.
...
Code Block | ||
---|---|---|
| ||
:a # Adds text after the cursor :A # Adds test after the current line :i # Inserts text before the cursor :I # Inserts text at the beginning of the current line :o # Inserts a blank line after the current line :O # Inserts a blank line after before the current line :x # Deletes one character under the cursor :X # Deletes one character behind the cursor :dd # Deletes the current line :5dd # Deletes five lines from the current line :r # Replaces the character under the cursor :R # Replaces the existing text :y # Copies the current line :p # Pastes the copied line :u # Undoes the last change :U # Undoes all changes on the current line |
...
Importing files into vi:
Code Block | ||
---|---|---|
| ||
vi vi_file ESC :r import_file |
6.
Searching and replacing in vi
...
...
find a string of text in vi
...
vi vi_file
Esc |
---|
Code Block | ||
---|---|---|
| ||
/text # look for "text" |
...
get the first result, and press N to find the next one.6b.
search and replace in vi:
vi vi_filename
Esc |
---|
Code Block | ||
---|---|---|
| ||
:%s/text/newtext # replace "text" with "newtext" |