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~~~
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
From this description, you can understand that in Vim, the empty character
<Nul>
(ASCII 0)在内存中是作为<NL>
(newline) is processed.'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
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
--------