1.
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.2a.
Saving in vi:
Get out of input mode and into command mode
Code Block |
---|
|
:w vi_file_name # save theas a vi file |
2b.If you have saved the file once, just use
...
3.
Exiting 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
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
Code Block |
---|
|
:%s/text/newtext # replace "text" with "newtext" |
Turning off the color in vi