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~~~
我想大声告诉你2017-05-16 16:44:44
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 textLine break problem in Vim
===============
If you want to use s/n/n/g
会导致文件的换行全部被替换成 ^@
字符,按照通常的理解,s/n/n/g
不应该有任何的效果,但在 Vim 中,n
in Vim, the handling 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}
From this description, you can understand that in Vim, the empty character <Nul>
(ASCII 0)在内存中是作为 <NL>
(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 <Nul>
字符。在替换表达式中 'n' 会被解释为 <NL>
,于是在内部,<Nul>
会被输入,所以在替换的表达式中 'n' 不再表示 'new line' 或者 'end-of-line'。同样,在搜索表达式中,输入 "CTRL-@" 或者 "CTRL-V 000" 表示 <Nul>
字符,但是在内部,他们还是都被替换为 <NL>
进行处理的,这也就是为什么直接键入 CTRL-V CTRL-J(输入的是 <NL>
character itself) which has the same effect.
I think the reason for the confusion may be that Vi does not support <Nul>
字符,而 Vim 由 Vi 发展而来,作者可能为了方便,直接使用了 <NL>
用来表示 <Nul>
,这也就是 'NL-used-for-Nul' 的字面意思。与此同时,<NL>
原本的作用则使用 <CR>
as a replacement, which 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 represent <NL>
呢?答案就是 <CR>
,可以使用 CRTL-V CRTL-M(输入的是 <CR>
字符本身) 或者 'r',Vim 并不会在文件中直接输入 <CR>
字符,它会根据当前 ‘fileformat’ 的设置来决定使用 <CR>
(Mac),<NL>
(*nix) 还是 <CR><NL>
(dos) in a substitution expression.
References
--------