Replacing carriage return ^M with Enter

前端 未结 5 1442
一向
一向 2021-01-31 08:24

I know how to remove ^M in my files (%s/^M//g), but this one is just one line I\'d like to replace ^M with enter... what\'s the enter char

相关标签:
5条回答
  • 2021-01-31 08:35

    You can replace one character using r<CR> in normal mode.
    Or you can enter a "return" in command line mode by typing <C-v><CR>.

    0 讨论(0)
  • 2021-01-31 08:41

    In vim session try:

    :%s/^M//g
    

    Where ^M is achieved by ctrl+V+M keystrokes together.

    0 讨论(0)
  • 2021-01-31 08:58

    Similar to @ZyX and @anubhava, but assuming you're simply trying to remove the pesky carriage returns from a windows file, the following will suffice:

    :%s/\r//g
    
    0 讨论(0)
  • 2021-01-31 08:59

    :%s/\r//g only works when:

    • set ff=unix, which when done, automatically converts all CRLF to LF

    • set ff=dos and CR is a rogue char that is not preceded by LF, e.g., inserted with C-V C-M.

      CR in LF CR pairs will not be found.

    Therefore, if all you want is to convert every LF CR to LF, you should use:

    :set ff=unix
    :w
    
    0 讨论(0)
  • 2021-01-31 09:02

    To replace carriage return character (which is <C-m>) with line feed character (which is unix line break character) you should run a bit strange command:

    %s/\r/\r/g
    

    It looks like if it is doing nothing, but in regular expressions and double-quoted strings carriage returns are represented using \r and line feeds with \n, while in the replacement part of :s command and substitute() function they mean the opposite.

    Note that in terminal Enter produces <C-m>, so your initial request is not valid.

    0 讨论(0)
提交回复
热议问题