Why is \r a newline for Vim?

后端 未结 5 2030
执笔经年
执笔经年 2020-11-27 09:30

From question How to replace a character for a newline in Vim?. You have to use \\r when replacing text for a newline, like this

:%s/%/\\r/g
         


        
5条回答
  •  被撕碎了的回忆
    2020-11-27 09:52

    First of all, open :h :s to see the section "4.2 Substitute" of documentation on "Change". Here's what the command accepts:

    :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
    

    Notice the description about pattern and string

    For the {pattern} see |pattern|.
    {string} can be a literal string, or something
    special; see |sub-replace-special|.

    So now you know that the search pattern and replacement patterns follow different rules. If you follow the link to |pattern|, it takes you to the section that explains the whole regexp patterns used in Vim.

    Meanwhile, |sub-replace-special| takes you to the subsection of "4.2 Substitute", which contains the patterns for substitution, among which is \r for line break/split.

    (The shortcut to this part of manual is :h :s%)

提交回复
热议问题