I usually have to worry about escaping search parameters to get special characters to match literally, but this time I'm seeing a problem with my replacement string.

I want to take text like this:

@@ something @@ should_be_on_another_line

And turn it into this:

@@ something @@

/ @@ / matches the second @@ pair on each line just fine, but when I try this command:

:%s/ @@ / @@ \n/g

I actually end up with this:

@@ something @@^@ should_be_on_another_line

I tried escaping the @@ \n in the replacement as \@\@ \n, but that didn't help (same results).

1 Answer 1


You are close. The newline should be \r in your substitute command.


:%s/ @@ / @@ \r /g

from :help :%s

  <CR>        split line in two at this point
              (Type the <CR> as CTRL-V <Enter>)                  s<CR>
  \r          idem                                               s/\r
  \<CR>       insert a carriage-return (CTRL-M)
              (Type the <CR> as CTRL-V <Enter>)                  s/\<CR>
  \n          insert a <NL> (<NUL> in the file)
              (does NOT break the line)                          s/\n

EDIT: The issue is not about escaping the @ in your substitute command. The meaning of \n differs between the search or the replace side of the expression. The \n on the right side of the expression inserts ^@.

This SO question has more details, in particular: :h s/\n from this answer

  • Thanks; although I must say I'm not sure how that documentation explains any of this. \r is carriage return (aka <CR>) and \n is line feed (aka new line). I get that they would match different things, but shouldn't I be able to replace with either of them (and get different results)? Jul 20, 2017 at 19:10
  • 2
    Simplified: :%s/ @@ /&\r/g. See :h :s% for more information. Jul 20, 2017 at 19:30

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.