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
  • why do I need to escape the \+ in vim? Is there another way to do this? – lacostenycoder Jan 12 at 16:29
  • The escape prevents the + from being interpreted as a literal character. I don't know of any other way to do it. – Eric Mintz Jan 12 at 16:39
  • 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 agree to our terms of service, privacy policy and cookie policy

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