<p>I'm trying to replace each <code>,</code> in the current file by a new line:</p>

<pre><code>:%s/,/\n/g 
</code></pre>

<p>But it inserts what looks like a <code>^@</code> instead of an actual newline. The file is not in DOS mode or anything.</p>

<p>What should I do?</p>

<p>If you are curious, like me, check the question <em><a href="https://stackoverflow.com/questions/71417/why-is-r-a-newline-for-vim">Why is \r a newline for Vim?</a></em> as well.</p>

[{'answer_id': 71334, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 13, 'selected': True, 'text': '<h2>Use <code>\\r</code> instead of <code>\\n</code>.</h2>\n<p>Substituting by <code>\\n</code> inserts a null character into the text. To get a newline, use <code>\\r</code>. When <em>searching</em> for a newline, you’d still use <code>\\n</code>, however. This asymmetry is due to the fact that <code>\\n</code> and <code>\\r</code> <a href="http://vim.wikia.com/wiki/Search_and_replace" rel="noreferrer">do slightly different things</a>:</p>\n<p><code>\\n</code> matches an end of line (newline), whereas <code>\\r</code> matches a carriage return. On the other hand, in substitutions <code>\\n</code> inserts a null character whereas <code>\\r</code> inserts a newline (more precisely, it’s treated as the input <kbd>CR</kbd>). 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). <code>xxd</code> shows a hexdump of the resulting file.</p>\n<pre><code>echo bar &gt; test\n(echo \'Before:\'; xxd test) &gt; output.txt\nvim test \'+s/b/\\n/\' \'+s/a/\\r/\' +wq\n(echo \'After:\'; xxd test) &gt;&gt; output.txt\nmore output.txt\n</code></pre>\n\n<pre><code>Before:\n0000000: 6261 720a                                bar.\nAfter:\n0000000: 000a 720a                                ..r.\n</code></pre>\n<p>In other words, <code>\\n</code> has inserted the byte 0x00 into the text; <code>\\r</code> has inserted the byte 0x0a.</p>\n'}, {'answer_id': 71342, 'author': 'Lasar', 'author_id': 9438, 'author_profile': 'https://Stackoverflow.com/users/9438', 'pm_score': 5, 'selected': False, 'text': '<p><code>\\r</code> can do the work here for you. </p>\n'}, {'answer_id': 71388, 'author': 'dogbane', 'author_id': 7412, 'author_profile': 'https://Stackoverflow.com/users/7412', 'pm_score': 6, 'selected': False, 'text': '<p>You need to use:</p>\n\n<pre><code>:%s/,/^M/g\n</code></pre>\n\n<p>To get the <code>^M</code> character, press <kbd>Ctrl</kbd> + <kbd>v</kbd> followed by <kbd>Enter</kbd>.</p>\n'}, {'answer_id': 71474, 'author': 'grantc', 'author_id': 11845, 'author_profile': 'https://Stackoverflow.com/users/11845', 'pm_score': 5, 'selected': False, 'text': '<p>With Vim on Windows, use <kbd>Ctrl</kbd> + <kbd>Q</kbd> in place of <kbd>Ctrl</kbd> + <kbd>V</kbd>.</p>\n'}, {'answer_id': 136915, 'author': 'Logan', 'author_id': 1127433, 'author_profile': 'https://Stackoverflow.com/users/1127433', 'pm_score': 8, 'selected': False, 'text': "<p>Here's the trick:</p>\n<p>First, set your Vi(m) session to allow pattern matching with special characters (i.e.: newline). It's probably worth putting this line in your .vimrc or .exrc file:</p>\n<pre><code>:set magic\n</code></pre>\n<p>Next, do:</p>\n<pre><code>:s/,/,^M/g\n</code></pre>\n<p>To get the <code>^M</code> character, type <kbd>Ctrl</kbd> + <kbd>V</kbd> and hit <kbd>Enter</kbd>. Under Windows, do <kbd>Ctrl</kbd> + <kbd>Q</kbd>, <kbd>Enter</kbd>. The only way I can remember these is by remembering how little sense they make:</p>\n<blockquote>\n<p>A: <em>What would be the worst control-character to use to represent a newline?</em></p>\n<p>B: <em>Either <code>q</code> (because it usually means &quot;Quit&quot;) or <code>v</code> because it would be so easy to type <kbd>Ctrl</kbd> + <kbd>C</kbd> by mistake and kill the editor.</em></p>\n<p>A: <em>Make it so.</em></p>\n</blockquote>\n"}, {'answer_id': 7324063, 'author': 'rickfoosusa', 'author_id': 931265, 'author_profile': 'https://Stackoverflow.com/users/931265', 'pm_score': 4, 'selected': False, 'text': '<p>From <a href="http://en.wikipedia.org/wiki/Eclipse_%28software%29" rel="nofollow noreferrer">Eclipse</a>, the <code>^M</code> characters can be embedded in a line, and you want to convert them to newlines.</p>\n\n<pre><code>:s/\\r/\\r/g\n</code></pre>\n'}, {'answer_id': 9134411, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s the answer that worked for me. From this guy:</p>\n\n<p>----quoting <em><a href="http://jaysonlorenzen.wordpress.com/2009/04/28/use-vi-editor-to-insert-newline-char-in-replace/" rel="nofollow noreferrer">Use the vi editor to insert a newline char in replace</a></em></p>\n\n<hr>\n\n<p>Something else I have to do and cannot remember and then have to look up.</p>\n\n<p>In vi, to insert a newline character in a search and replace, do the following:</p>\n\n<pre><code>:%s/look_for/replace_with^M/g\n</code></pre>\n\n<p>The command above would replace all instances of “look_for” with “replace_with\\n” (with \\n meaning newline).</p>\n\n<p>To get the “^M”, enter the key combination <kbd>Ctrl</kbd> + <kbd>V</kbd>, and then after that (release all keys) press the <kbd>Enter</kbd> key.</p>\n\n<hr>\n'}, {'answer_id': 9172870, 'author': 'Kiran K Telukunta', 'author_id': 888574, 'author_profile': 'https://Stackoverflow.com/users/888574', 'pm_score': 3, 'selected': False, 'text': '<p>But if one has to substitute, then the following thing works:</p>\n\n<pre><code>:%s/\\n/\\r\\|\\-\\r/g\n</code></pre>\n\n<p>In the above, every next line is substituted with next line, and then <code>|-</code> and again a new line. This is used in wiki tables.</p>\n\n<p>If the text is as follows:</p>\n\n<pre><code>line1\nline2\nline3\n</code></pre>\n\n<p>It is changed to</p>\n\n<pre><code>line1\n|-\nline2\n|-\nline3\n</code></pre>\n'}, {'answer_id': 9220288, 'author': 'Evan Donovan', 'author_id': 263877, 'author_profile': 'https://Stackoverflow.com/users/263877', 'pm_score': 3, 'selected': False, 'text': "<p>If you need to do it for a whole file, it was also suggested to me that you could try from the command line:</p>\n\n<pre><code>sed 's/\\\\n/\\n/g' file &gt; newfile\n</code></pre>\n"}, {'answer_id': 18961239, 'author': 'sjas', 'author_id': 805284, 'author_profile': 'https://Stackoverflow.com/users/805284', 'pm_score': 7, 'selected': False, 'text': '<p>In the syntax <code>s/foo/bar</code>, <code>\\r</code> and <code>\\n</code> have different meanings, depending on context.</p>\n<hr />\n<h2>Short:</h2>\n<p>For <code>foo</code>:<br/></p>\n<p><code>\\r</code> == &quot;carriage return&quot; (<code>CR</code> / <code>^M</code>)<br/>\n<code>\\n</code> == matches &quot;line feed&quot; (<code>LF</code>) on Linux/Mac, and <code>CRLF</code> on Windows<br/></p>\n<p>For <code>bar</code>:<br/></p>\n<p><code>\\r</code> == produces <code>LF</code> on Linux/Mac, <code>CRLF</code> on Windows<br/>\n<code>\\n</code> == &quot;null byte&quot; (<code>NUL</code> / <code>^@</code>)<br/></p>\n<p>When editing files in linux (i.e. on a webserver) that were initially created in a windows environment and uploaded (i.e. FTP/SFTP) - all the <code>^M</code>\'s you see in vim, are the <code>CR</code>\'s which linux does not translate as it uses only <code>LF</code>\'s to depict a line break.</p>\n<hr />\n<h2>Longer (with ASCII numbers):</h2>\n<p>\n<code>NUL</code> == 0x00 == 0 == <kbd>Ctrl</kbd> + <kbd>@</kbd> == <code>^@</code> shown in vim<br/>\n<code>LF</code> == 0x0A == 10 == <kbd>Ctrl</kbd> + <kbd>J</kbd><br/>\n<code>CR</code> == 0x0D == 13 == <kbd>Ctrl</kbd> + <kbd>M</kbd> == <code>^M</code> shown in vim</p>\n<p>Here is a list of the <a href="http://www.cs.tut.fi/%7Ejkorpela/chars/c0.html" rel="noreferrer">ASCII control characters</a>. Insert them in Vim via <kbd>Ctrl</kbd> + <kbd>V</kbd>,<kbd>Ctrl</kbd> + <kbd>---key---</kbd>.</p>\n<p>In Bash or the other Unix/Linux shells, just type <kbd>Ctrl</kbd> + <kbd>---key---</kbd>.</p>\n<p>Try <kbd>Ctrl</kbd> + <kbd>M</kbd> in Bash. It\'s the same as hitting <kbd>Enter</kbd>, as the shell realizes what is meant, even though Linux systems use line feeds for line delimiting.</p>\n<p>To insert literal\'s in bash, prepending them with <kbd>Ctrl</kbd> + <kbd>V</kbd> will also work.</p>\n<p>Try in Bash:</p>\n<pre><code>echo ^[[33;1mcolored.^[[0mnot colored.\n</code></pre>\n<p>This uses <a href="http://en.wikipedia.org/wiki/ANSI_escape_code" rel="noreferrer">ANSI escape sequences</a>. Insert the two <code>^[</code>\'s via <kbd>Ctrl</kbd> + <kbd>V</kbd>, <kbd>Esc</kbd>.</p>\n<p>You might also try <kbd>Ctrl</kbd> + <kbd>V</kbd>,<kbd>Ctrl</kbd> + <kbd>M</kbd>, <kbd>Enter</kbd>, which will give you this:</p>\n<pre><code>bash: $\'\\r\': command not found\n</code></pre>\n<p>Remember the <code>\\r</code> from above? :&gt;</p>\n<p>This <a href="http://www.cs.tut.fi/%7Ejkorpela/chars/c0.html" rel="noreferrer">ASCII control characters</a> list is different from a complete <a href="http://ascii-code.com/" rel="noreferrer">ASCII symbol table</a>, in that the control characters, which are inserted into a console/pseudoterminal/Vim via the <kbd>Ctrl</kbd> key (haha), can be found there.</p>\n<p>Whereas in C and most other languages, you usually use the octal codes to represent these \'characters\'.</p>\n<p>If you really want to know where all this comes from: <em><a href="http://www.linusakesson.net/programming/tty/" rel="noreferrer">The TTY demystified</a></em>. This is the best link you will come across about this topic, but beware: There be dragons.</p>\n<hr />\n<p><em>TL;DR</em></p>\n<p>Usually <code>foo</code> = <code>\\n</code>, and <code>bar</code> = <code>\\r</code>.</p>\n'}, {'answer_id': 29514339, 'author': 'codeshot', 'author_id': 962394, 'author_profile': 'https://Stackoverflow.com/users/962394', 'pm_score': 4, 'selected': False, 'text': '<p>This is the best answer for the way I think, but it would have been nicer in a table:</p>\n\n<p><em><a href="https://stackoverflow.com/questions/71417/why-is-r-a-newline-for-vim/12389839#12389839">Why is \\r a newline for Vim?</a></em></p>\n\n<p>So, rewording:</p>\n\n<p>You need to use <code>\\r</code> to use a line feed (ASCII <code>0x0A</code>, the Unix newline) in a regex replacement, but that is peculiar to the replacement - you should normally continue to expect to use <code>\\n</code> for line feed and <code>\\r</code> for carriage return.</p>\n\n<p>This is because Vim used <code>\\n</code> in a replacement to mean the NIL character (ASCII <code>0x00</code>). You might have expected NIL to have been <code>\\0</code> instead, freeing <code>\\n</code> for its usual use for line feed, but <code>\\0</code> already has a meaning in regex replacements, so it was shifted to <code>\\n</code>. Hence then going further to also shift the newline from <code>\\n</code> to <code>\\r</code> (which in a regex pattern is the carriage return character, ASCII <code>0x0D</code>).</p>\n\n<pre>\nCharacter                | ASCII code | C representation | Regex match | Regex replacement\n-------------------------+------------+------------------+-------------+------------------------\nnil                      | 0x00       | \\0               | \\0          | \\n\nline feed (Unix newline) | 0x0a       | \\n               | \\n          | \\r\ncarriage return          | 0x0d       | \\r               | \\r          | &lt;unknown&gt;\n</pre>\n\n<p>NB: <code>^M</code> (<kbd>Ctrl</kbd> + <kbd>V</kbd> <kbd>Ctrl</kbd> + <kbd>M</kbd> on Linux) inserts a newline when used in a regex replacement rather than a carriage return as others have advised (I just tried it).</p>\n\n<p>Also note that Vim will translate the line feed character when it saves to file based on its file format settings and that might confuse matters.</p>\n'}, {'answer_id': 73473053, 'author': 'Rajashekhar Meesala', 'author_id': 3888182, 'author_profile': 'https://Stackoverflow.com/users/3888182', 'pm_score': 1, 'selected': False, 'text': '<p>in vim editor the following command successfully replaced \\n with new line</p>\n<pre><code>:%s/\\\\n/\\r/g\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/71323', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5190/']