Regular Expressions - What is the difference between vim's \n and \r
高洛峰
高洛峰 2017-05-16 16:42:44
0
1
837

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

:s/\n/\r/

Nothing happens. In theory, I can replace all \n with \r, and then search again

/\r

Still nothing, only

/\n

It has value, please find the principle~~~

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (1)
我想大声告诉你

I wrote a note before, but it has not been made public. I am posting it here for reference.

(The original text is in markdown format, which is not fully supported by SF, but it does not affect reading)

Note text

Line break problem in Vim
===============

If you want to uses/n/n/g会导致文件的换行全部被替换成^@字符,按照通常的理解,s/n/n/g不应该有任何的效果,但在 Vim 中,nin Vim, the handling is slightly different.

:help NL-used-for-Nul

[ ](http://vimdoc.sourceforge.net/htmldoc...) characters in the file are stored as [ ](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 [ ](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 [ ](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 [ ](http://vimdoc.sourceforge.net/htmldoc...), thus also searches for a [ ](http://vimdoc.sourceforge.net/htmldoc...) in the file. {Vi cannot handle [ ](http://vimdoc.sourceforge.net/htmldoc...) characters in the file at all}

From this description, you can understand that in Vim, the empty character(ASCII 0)在内存中是作为(newline) is processed.

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

'n' is matching a string in the search expression, so the literal 'new line' string will be matched, not the字符。在替换表达式中 'n' 会被解释为,于是在内部,会被输入,所以在替换的表达式中 'n' 不再表示 'new line' 或者 'end-of-line'。同样,在搜索表达式中,输入 "CTRL-@" 或者 "CTRL-V 000" 表示字符,但是在内部,他们还是都被替换为进行处理的,这也就是为什么直接键入 CTRL-V CTRL-J(输入的是character itself) which has the same effect.

I think the reason for the confusion may be that Vi does not support字符,而 Vim 由 Vi 发展而来,作者可能为了方便,直接使用了用来表示,这也就是 'NL-used-for-Nul' 的字面意思。与此同时,原本的作用则使用as a replacement, which is the literal meaning of 'CR-used-for-NL'.

:help CR-used-for-NL

When 'fileformat' is "mac", characters in the file are stored as characters internally. In the text they are shown as "^J". Otherwise this works similar to the usage of < NL> for a .
When working with expression evaluation, a character in the pattern matches a 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 represent呢?答案就是,可以使用 CRTL-V CRTL-M(输入的是字符本身) 或者 'r',Vim 并不会在文件中直接输入字符,它会根据当前 ‘fileformat’ 的设置来决定使用(Mac),(*nix) 还是(dos) in a substitution expression.

References
--------

  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 newlines](http://blog.longwin.com.tw/2008/08/vi...)
  4. [How to replace a character for a newline in Vim?](http://stackoverflow.com/questions/71...)
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!