I'm trying to replace each , in the current file by a new line:
:%s/,/\n/g
But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.
What should I do?
If you are curious, like me, check the question Why is \r a newline for Vim? as well.
Best Solution
Use
\rinstead of\n.Substituting by
\ninserts a null character into the text. To get a newline, use\r. When searching for a newline, you’d still use\n, however. This asymmetry is due to the fact that\nand\rdo slightly different things:\nmatches an end of line (newline), whereas\rmatches a carriage return. On the other hand, in substitutions\ninserts a null character whereas\rinserts a newline (more precisely, it’s treated as the input CR). Here’s a small, non-interactive example to illustrate this, using the Vim command line feature (in other words, you can copy and paste the following into a terminal to run it).xxdshows a hexdump of the resulting file.In other words,
\nhas inserted the byte 0x00 into the text;\rhas inserted the byte 0x0a.