为什么\ r是Vim的换行符?

时间:2020-03-05 18:55:41  来源:igfitidea点击:

来自问题如何在Vim中替换换行符?像这样在换行符中替换文本时,必须使用\ r

:%s/%/\r/g

但是,当用字符替换行尾和换行符时,我们可以这样做:

:%s/\n/%/g

手册的哪一部分记录了这些行为,其背后的原因是什么?

解决方案

回答

:help NL用于Nul

Technical detail:
  
  <Nul> characters in the file are stored as <NL> in memory. In the display
  they are shown as "^@".  The translation is done when reading and writing
  files.  To match a <Nul> with a search pattern you can just enter CTRL-@ or
  "CTRL-V 000".  This is probably just what you expect.  Internally the
  character is replaced with a <NL> in the search pattern.  What is unusual is
  that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
  in the file.  {Vi cannot handle <Nul> characters in the file at all}

回答

从vim文档中找到关于模式的信息:

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