Which regex do I have to use in VIM to replace all words 'to' from a text with the word 'foo'? (I don't want to replace 'to' when it happens in a word like 'together').

link|improve this question

68% accept rate
feedback

2 Answers

up vote 7 down vote accepted

this should work:

:%s/\<to\>/foo/g

Using \< and \> ensure that "to" will only be replaced if it is an entire word. \< matches the start of a word and \> the end. This way it won't turn your "tod" into "food" :)

link|improve this answer
Much nicer than my solution for sure :-) – Raphink Dec 28 '09 at 16:16
feedback

I'm sure there's a simpler way, but:

:%s/\(\w\)to\(\w\)/\1foo\2/gc

would do the trick

EDIT: John T has posted the simpler way :D

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.