17

I copy pasted cells of a long google spreadsheet into a txt file that is a list of email addresses separated by commas. There are many "blank" cells as well, i.e. a blank space surrounded by commas. So I could have the following list:

bob@aol.com, ,john@aol.com, , , , email@email.com

In vim, when I try to add separate each address by a new line with this command:

:%s/, /,\n/g

instead of adding a new line after the comman, I get "^@" instead.

I know this has something to do with character sets, but I don't know how to fix it.

mehulkar
  • 4,624
  • 4
  • 30
  • 50

1 Answers1

17

In :s's replacement field, you need to use \r not \n for newline characters.

^@ is the ASCII null character. Vim internally uses \r for newlines (which is ^M), and \n for ASCII null, so in the replacement, if you use \n you're getting those null characters instead of newlines. See also :h sub-replace-special

Julian
  • 3,207
  • 13
  • 25
  • Can you explain why his `\n` shows up as `^@` ? – Anirudh Ramanathan Jul 29 '12 at 07:46
  • 4
    `^@` is the ASCII null character. Vim internally uses `\r` for newlines (which is `^M`), and `\n` for ASCII null, so in the replacement, if you use `\n` you're getting those null characters instead of newlines. See also `:h sub-replace-special`. – Julian Jul 29 '12 at 07:51
  • @Julian: I believe you should put that explanation in the answer. – Michał Górny Jul 29 '12 at 08:21
  • 3
    Vim internally does not use anything for newlines, it just have a list of lines and newline (well, `\n`, `\r\n` or `\r` depending on 'fileformat' option) appears at the end of the list item (or, with 'binary' and 'noendofline' options set, between two items of the list). `\r` character stands for itself, but two characters `\r` in *replacement string* stand for newline. `\n` standing for NULL is true, there is even `:h NL-used-for-Nul`. – ZyX Jul 29 '12 at 13:40