most recent 30 from stackoverflow.com 2026-03-01T05:30:19Z https://stackoverflow.com/feeds/question/71323 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stackoverflow.com/q/71323 2495 Vinko Vrsalovic https://stackoverflow.com/users/5190 2008-09-16T11:19:39Z 2025-09-26T11:46:56Z <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> https://stackoverflow.com/questions/71323/-/71334#71334 3226 Konrad Rudolph https://stackoverflow.com/users/1968 2008-09-16T11:21:03Z 2020-08-31T13:48:59Z <h2>Use <code>\r</code> instead of <code>\n</code>.</h2> <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> <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> <pre><code>echo bar &gt; test (echo 'Before:'; xxd test) &gt; output.txt vim test '+s/b/\n/' '+s/a/\r/' +wq (echo 'After:'; xxd test) &gt;&gt; output.txt more output.txt </code></pre> <pre><code>Before: 0000000: 6261 720a bar. After: 0000000: 000a 720a ..r. </code></pre> <p>In other words, <code>\n</code> has inserted the byte 0x00 into the text; <code>\r</code> has inserted the byte 0x0a.</p> https://stackoverflow.com/questions/71323/-/71342#71342 46 Lasar https://stackoverflow.com/users/9438 2008-09-16T11:21:34Z 2015-04-30T15:48:15Z <p><code>\r</code> can do the work here for you. </p> https://stackoverflow.com/questions/71323/-/71388#71388 66 dogbane https://stackoverflow.com/users/7412 2008-09-16T11:30:32Z 2019-11-15T09:52:50Z <p>You need to use:</p> <pre><code>:%s/,/^M/g </code></pre> <p>To get the <code>^M</code> character, press <kbd>Ctrl</kbd> + <kbd>v</kbd> followed by <kbd>Enter</kbd>.</p> https://stackoverflow.com/questions/71323/-/71474#71474 25 grantc https://stackoverflow.com/users/11845 2008-09-16T11:45:40Z 2019-11-15T09:53:45Z <p>With Vim on Windows, use <kbd>Ctrl</kbd> + <kbd>Q</kbd> in place of <kbd>Ctrl</kbd> + <kbd>V</kbd>.</p> https://stackoverflow.com/questions/71323/-/136915#136915 255 Logan https://stackoverflow.com/users/1127433 2008-09-25T23:40:19Z 2019-11-15T09:57:12Z <p>Here's the trick:</p> <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> <pre><code>:set magic </code></pre> <p>Next, do:</p> <pre><code>:s/,/,^M/g </code></pre> <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> <blockquote> <p>A: <em>What would be the worst control-character to use to represent a newline?</em></p> <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> <p>A: <em>Make it so.</em></p> </blockquote> https://stackoverflow.com/questions/71323/-/7324063#7324063 11 rickfoosusa https://stackoverflow.com/users/931265 2011-09-06T17:53:05Z 2019-11-15T09:58:24Z <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> <pre><code>:s/\r/\r/g </code></pre> https://stackoverflow.com/questions/71323/-/9134411#9134411 7 user407749 https://stackoverflow.com/users/0 2012-02-03T19:31:15Z 2019-11-15T10:01:32Z <p>Here's the answer that worked for me. From this guy:</p> <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> <hr> <p>Something else I have to do and cannot remember and then have to look up.</p> <p>In vi, to insert a newline character in a search and replace, do the following:</p> <pre><code>:%s/look_for/replace_with^M/g </code></pre> <p>The command above would replace all instances of “look_for” with “replace_with\n” (with \n meaning newline).</p> <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> <hr> https://stackoverflow.com/questions/71323/-/9172870#9172870 9 Kiran K Telukunta https://stackoverflow.com/users/888574 2012-02-07T08:09:42Z 2019-11-15T10:03:03Z <p>But if one has to substitute, then the following thing works:</p> <pre><code>:%s/\n/\r\|\-\r/g </code></pre> <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> <p>If the text is as follows:</p> <pre><code>line1 line2 line3 </code></pre> <p>It is changed to</p> <pre><code>line1 |- line2 |- line3 </code></pre> https://stackoverflow.com/questions/71323/-/9220288#9220288 5 Evan Donovan https://stackoverflow.com/users/263877 2012-02-09T23:01:29Z 2019-11-15T10:03:48Z <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> <pre><code>sed 's/\\n/\n/g' file &gt; newfile </code></pre> https://stackoverflow.com/questions/71323/-/18961239#18961239 130 sjas https://stackoverflow.com/users/805284 2013-09-23T14:00:45Z 2020-07-31T08:59:06Z <p>In the syntax <code>s/foo/bar</code>, <code>\r</code> and <code>\n</code> have different meanings, depending on context.</p> <hr /> <h2>Short:</h2> <p>For <code>foo</code>:<br/></p> <p><code>\r</code> == &quot;carriage return&quot; (<code>CR</code> / <code>^M</code>)<br/> <code>\n</code> == matches &quot;line feed&quot; (<code>LF</code>) on Linux/Mac, and <code>CRLF</code> on Windows<br/></p> <p>For <code>bar</code>:<br/></p> <p><code>\r</code> == produces <code>LF</code> on Linux/Mac, <code>CRLF</code> on Windows<br/> <code>\n</code> == &quot;null byte&quot; (<code>NUL</code> / <code>^@</code>)<br/></p> <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> <hr /> <h2>Longer (with ASCII numbers):</h2> <p> <code>NUL</code> == 0x00 == 0 == <kbd>Ctrl</kbd> + <kbd>@</kbd> == <code>^@</code> shown in vim<br/> <code>LF</code> == 0x0A == 10 == <kbd>Ctrl</kbd> + <kbd>J</kbd><br/> <code>CR</code> == 0x0D == 13 == <kbd>Ctrl</kbd> + <kbd>M</kbd> == <code>^M</code> shown in vim</p> <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> <p>In Bash or the other Unix/Linux shells, just type <kbd>Ctrl</kbd> + <kbd>---key---</kbd>.</p> <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> <p>To insert literal's in bash, prepending them with <kbd>Ctrl</kbd> + <kbd>V</kbd> will also work.</p> <p>Try in Bash:</p> <pre><code>echo ^[[33;1mcolored.^[[0mnot colored. </code></pre> <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> <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> <pre><code>bash: $'\r': command not found </code></pre> <p>Remember the <code>\r</code> from above? :&gt;</p> <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> <p>Whereas in C and most other languages, you usually use the octal codes to represent these 'characters'.</p> <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> <hr /> <p><em>TL;DR</em></p> <p>Usually <code>foo</code> = <code>\n</code>, and <code>bar</code> = <code>\r</code>.</p> https://stackoverflow.com/questions/71323/-/29514339#29514339 21 codeshot https://stackoverflow.com/users/962394 2015-04-08T12:15:22Z 2019-11-15T10:41:14Z <p>This is the best answer for the way I think, but it would have been nicer in a table:</p> <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> <p>So, rewording:</p> <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> <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> <pre> Character | ASCII code | C representation | Regex match | Regex replacement -------------------------+------------+------------------+-------------+------------------------ nil | 0x00 | \0 | \0 | \n line feed (Unix newline) | 0x0a | \n | \n | \r carriage return | 0x0d | \r | \r | &lt;unknown&gt; </pre> <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> <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> https://stackoverflow.com/questions/71323/-/73473053#73473053 4 Rajashekhar Meesala https://stackoverflow.com/users/3888182 2022-08-24T12:18:43Z 2022-08-24T12:18:43Z <p>in vim editor the following command successfully replaced \n with new line</p> <pre><code>:%s/\\n/\r/g </code></pre>