Unix VI – search & replace reference

?When using the VI editor on most unix systems a common command you will use is the search and replace.  The % indicates that you want to search the entire file, you can also use 1,$ to indicate from line 1 to the end.  If you just say :s it will only search the current line. If you want to search from the current line to end of a file you can type :.,$s the “.” Indicates the current line. Lastly the g at the end of the command indicates “global” so all occurances will be changed.  If you omit the g it will only change the first occurrence of search_string.

 :%s/{search_string}/{replacement_string}/g

 If you are searching for a string or replace a string the has a “/” in it, you can have to prefix the “/” with a “\” it can get somewhat messy but it works!

For example

:%s/\/etc/\/opt/g

This will search the entire file for /etc and change it to /opt