vi Search and Replace

It’s sad to say I don’t use the vi search & replace enough to have it ingrained where it should be, so here’s to open documentation:

Adding the “how to” search for those who might need it:

Note: Search wraps around at end of file

To search STR forward: /STR
To search STR backwards: ?STR

To repeat the search: n
To repeat search in opposite direction: N ([SHIFT]-n)

Replace
Replace: Same as with sed, Replace OLD with NEW:

First occurrence on current line: :s/OLD/NEW
Globally (all) on current line: :s/OLD/NEW/g
Between two line numbers #,#: :#,#s/OLD/NEW/g
Every occurrence in file: :%s/OLD/NEW/g

Discussion Area - Leave a Comment