Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an ipv6 hosts file. No I want to add a comment symbol # to each line that having "".

How could I do this in vim? I thought it would be something like %s/^.*google\.com\.hk/^#???/.


share|improve this question
up vote 6 down vote accepted

Use & in the replacement text to stand for the whole original string:


or, to avoid replacing things like and

%s/^.*[ .]google\.com\.hk\( \|$\)/#&/

Alternatively, use the g command to apply an s command to all matching lines:

g/[ .]google\.com\.hk\( \|$\)/ s/^/#/
share|improve this answer
what about \< and \> word boundary assertions? – Philip Potter Aug 28 '10 at 14:00
@Philip: They would help only if . and - were word constituent characters, which is not the case by default (and would render b/e/w not usefully different from B/E/W). – Gilles Aug 28 '10 at 14:17

Like this:

%s/\(^.*google\.com\.hk\)/# \1/

This tells VIM to search for what's in the parentheses, in this case ^.*google\.com\.hk, and put that into the \1 variable. Then you replace all that you found before with # followed by \1.

Alternatively, you could do:

%s/^.*google\.com\.hk/# &/

Where the & is shorthand for whatever was just matched

share|improve this answer

Your Answer


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.