4

I want to replace all words containing a string in a file using vim. how can it be done in vi editor ?

melvil james
  • 592
  • 7
  • 18

1 Answers1

16

You can use the substitution command like this:

:%s/before/after/g

where

  • %s is the substitution
  • before is the string to match
  • after is the replacement
  • g is the global flag (to replace all occurrences)

If you are matching substrings, you can use

:%s/\<\w*substr\w*\>/newword/g

where

  • \< matches the word boundary
  • \w* matches a word character (0 or more)
  • substr is your substring
  • > matches the word boundary end
  • newword is the new word to replace
  • g is the global replace
ergonaut
  • 6,929
  • 1
  • 17
  • 47