Tuesday, May 13, 2008

Replacing words in vi

To replace word in vi, the below steps can be used(replace OLD with NEW). Please make sure you are in command(normal) mode:

  1. to replace first occurrence of OLD to NEW on current line
    • :s/OLD/NEW
  2. to replace all occurrence of OLD to NEW on current line
    • :s/OLD/NEW/g
  3. to replace all occurrence of OLD to NEW between two line numbers (# are the line numbers)
    • :#,#s/OLD/NEW/g
  4. to replace every occurrence of OLD to NEW on current file
    • :%s/OLD/NEW/g

No comments: