I would like to write a search and replace that only operates on lines that are found in a separate search. So, for example, lets say I want to replace all occurrences of "long" with "int" but only on lines that have the word "key" on them.

:g/key/s/long/int/g

That's

:g/     on every line that matches
key     this regexp (the literal "key")
/       do the following action
s/      substitute
long    this regexp (the literal "long" in this case)
/       with
int     this result (the literal "int")
/g      and do it for all the occurances of "long"
        in the line

If you want to avoid problems with things like "longbottom",
or "singalong" becoming things like "intbottom" and
"singaint", you can use

:g/key/s/\<long\>/int/g

which will ensure that "long" occurs as a "word" (as defined
I *think* by the 'iskeyword' property)

        :help \<
        :help iskeyword
        :help :g


I could probably write out a script to do this, but I'm
betting there is a really slick way for Vim to do this
without a script.

Vim is full of slickness :)

-tim




Reply via email to