Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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').

share|improve this question
add comment

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" :)

share|improve this answer
    
Much nicer than my solution for sure :-) –  ℝaphink Dec 28 '09 at 16:16
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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