Vi Editor Tips and Tricks
By: Tariq Nazir (http://tariqnazir.tripod.com)
For introduction to vi editor and commands, click here
1- Search and Replace
Search STRING forward : / STRING.
Search STRING backward: ? STRING.
Repeat search: n
Repeat search in opposite direction: N (SHIFT-n)
Search and Replace
First occurrence on current line: :s/OLD/NEW
Globally (all) on current line: :s/OLD/NEW/g
Between two lines #,#: :#,#s/OLD/NEW/g
Every occurrence in file: :%s/OLD/NEW/g
2- Reading the output from a command into current file.
In a vi session, move the cursor to the position in the file where you want to bring the output of a command and run the command like:
:r !df -h
Here df command is used for example. Replace it with your command.
3- To sort contents of the file.
Move the cursor to the line where apple is located, then run the following
. - represents the current line
$ - represents the last line of the file
If you don't want to replace the text, but want to see the sorted output, do the following:
:.,$w !sort | more
Introduction to vi editor