What is the difference between \n and \r of vim

  question, vim

In vim, you need to find \n to replace the newline character, but \r to replace it with a newline character
That is to say


Nothing happened. In theory, I have already replaced all \n with \r, and then look for it again


Still nothing, only


Value, principle ~ ~ ~

I have written a note before and it has not been made public. It is put out here for reference.

(The original text is markdown format, which SF does not fully support, but does not affect reading)


The Problem of Line Break in Vim

In Vim, if you want to uses/\n/\n/gThis will cause all line breaks in the file to be replaced by^@Characters, as is commonly understood,s/\n/\n/gIt should not have any effect, but in Vim,\nThe treatment is slightly different.

:help NL-used-for-Nul

[<Nul>](http://vimdoc.sourceforge.net/htmldoc …) characters in the file are stored as [<NL>](http://vimdoc.sourceforge.net/htmldoc …) in memory. In the display they are shown as “^@“. The translation is done when reading and writing files. To match a [<Nul>](http://vimdoc.sourceforge.net/htmldoc …) with a search pattern you can just enter [CTRL-@](http://www.vim.org/htmldoc/insert.htm …) or “[CTRL-V](http://www.vim.org/htmldoc/insert.htm …) 000″. This is probably just what you expect. Internally the character is replaced with a [<NL>](http://vimdoc.sourceforge.net/htmldoc …) in the search pattern. What is unusual is that typing [CTRL-V](http://www.vim.org/htmldoc/insert.htm …) [CTRL-J](http://www.vim.org/htmldoc/insert.htm …) also inserts a [<NL>](http://vimdoc.sourceforge.net/htmldoc …), thus also searches for a [<Nul>](http://vimdoc.sourceforge.net/htmldoc …) in the file. {Vi cannot handle [<Nul>](http://vimdoc.sourceforge.net/htmldoc …) characters in the file at all}

It can be seen from this description that in Vim, empty characters<Nul>(ASCII 0) is used in memory as<NL>(newline) for processing.

\n matches an end-of-line
When matching in a string instead of buffer text a literal newline character is matched.

\n’ matches a string in the search expression, so the literal’ new line’ string will be matched to instead of<Nul>Characters. In the replacement expression’ \n’ will be interpreted as<NL>So inside,<Nul>Will be entered, so’ \n’ no longer represents’ new line’ or’ end-of-line’ in the replacement expression. Similarly, in the search expression, enter “CTRL-@” or “CTRL-V 000” to indicate<Nul>Characters, but internally, they are all replaced by<NL>Processing, which is why directly type CTRL-V CTRL-J (input is<NL>Character itself) also has the same effect.

I think Vi does not support the reason why there is a bit of confusion.<Nul>Character, while Vim is developed from Vi, the author may use it directly for convenience.<NL>Used to indicate<Nul>This is the literal meaning of ‘nl-used-for-nul’. At the same time,<NL>The original function is to use<CR>This is the literal meaning of ‘cr-used-for-nl’.

:help CR-used-for-NL

When ‘fileformat’ is “mac”, <NL> characters in the file are stored as <CR> characters internally. In the text they are shown as “^J”. Otherwise this works similar to the usage of <NL> for a <Nul>.
When working with expression evaluation, a <NL> character in the pattern matches a <NL> in the string. The use of “\ n” (backslash n) to match a <NL> doesn’t work there, it only works to match text in the buffer.

So, how do we express in the replacement expression<NL>? The answer is<CR>, you can use CRTL-V CRTL-M (input is<CR>Character itself) or’ \r’, Vim will not be entered directly in the file<CR>Character, which will be used according to the current’ fileformat’ setting.<CR>(Mac),<NL>(\*nix) or<CR><NL>(dos)。

Reference material

  1. [Vim: \n vs. \r](http://stackoverflow.com/questions/35 …)
  2. [Why is \r a newline for Vim? ](http://stackoverflow.com/questions/71 …)
  3. [Vim uses regex to replace “”with line feed] (http://blog.longwin.com.tw/2008/08/vi …)
  4. [How to replace a character for a newline in Vim? ](http://stackoverflow.com/questions/71 …)