I created an example of text:

dog cat worm

And I want to replace everything but not the first word and without knowing what's there.

At first I tried this::%s/\(dog\)\(.*\)/\1/g

This works fine but when I change it to: :%s/\(\w\)\(.*\)/\1/g

it returns


For second word regexp :%s/.*\(cat\)\(.*\)/\1/g I get cat so it's just as I wanted but with :%s/\w\(\w\)\(.*\)/\1/g I get c

1 Answer 1


\w does not represent a word but merely a word-character.

You want :%s/\(\w\+\)\(.*\)/\1/g

Also, in this simple example the /g does nothing though

  • Ok it works for the first example but second one: :%s/\(\w\+\)\(\w\+\)\(.*\)/\2/ returns g from dog word. Anyway is there a way of not escaping all characters like +)(? Regex gets messy really quick because of this. Jun 21, 2017 at 10:37
  • Ok I figured it out: :%s/\(\w\+\)\s\(\w\+\)\(.*\)/\2/ The problem was with space character. As my regexp cover full line I needed to add space character as it separates words. Thanks for help! Jun 21, 2017 at 12:51
  • 1
    @A.Dziedziczak If you don't want to have to escape so many characters, use a "very magic" pattern. See :help /\v
    – Rich
    Jun 21, 2017 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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