phato.blog

Humble semi-technical blog of Pat Wangrungarun

Lookbehind / Lookahead in Vim

15 August 2013 in Vim · view history

Vim is just powerful, sometimes you might want to replace your code from something to everything or someone. This is, matching the whole (or partially) of a string and replace (partially) of the string with another word.

I’ve found this very nice post describing lookbehind/ahead in Vim clearly. Allow me to copy some of his content here.

something becomes someone

:%s/\(some\)\@<=thing/one/g

Searches for all strings starting with some, then matching thing changes thing into one.

something is not changed, but everything changes to everyone

:%s/\(some\)\@<!thing/one/g

Searches for all strings not starting with some, then matching thing changes thing into one.

something becomes everything

:%s/some\(thing\)\@=/every/g

Searches for all strings ending with thing, then matching some changes some into every.

something is not changed, but someone becomes everyone.

:%s/some\(thing\)\@!/every/g

Searches for all strings not ending with thing, then matching some changes some into every.