0

My text file looks like this

Lastname, Firstname
Doe, John

I'm trying to get it to end like this using find replace regex

Firstname Lastname
John Doe

This regex throws the error, although using find/replace in atom editor with same pattern works fine. What am I doing wrong?

:s/(\w+),\s(\w+)/$1 $2/:g
1

Try it like this:

:%s/\(\w\+\),\s\(\w\+\)/\2 \1/g

Or for simpler syntax without all the escapes use \v for very-magic ( thanks to @D.BenKnoble )

:%s/\v(\w+),\s(\w+)/\2 \1/g
  • Could you change \1 \2 to \2 \1? The TO wanted to switch Lastname and Firstname. – Ralf Jan 12 at 16:17
  • 1
    Good catch. Thanks. (done) – Eric Mintz Jan 12 at 16:19
  • why do I need to escape the \+ in vim? Is there another way to do this? – lacostenycoder Jan 12 at 16:29
  • 2
    @lacostenycoder with the very-magic switch, less needs escaping. I suggest you read help pattern – D. Ben Knoble Jan 12 at 17:08
  • 2
    @lacostenycoder %s/\v(\w+),\s(\w+)/\2 \1/g (i think) – D. Ben Knoble Jan 12 at 19:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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