简体   繁体   中英

Replacing carriage return ^M with Enter

我知道如何在我的文件中删除^M%s/^M//g ),但这只是一行,我想用输入替换^M ...什么是VIM中的输入字符(使用在commnad-line模式下)。

To replace carriage return character (which is <Cm> ) 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 <Cm> , so your initial request is not valid.

:%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, eg, inserted with CV CM .

    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

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

In vim session try:

:%s/^M//g

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

类似于@ZyX和@anubhava,但假设您只是想从Windows文件中删除讨厌的回车符,以下就足够了:

:%s/\r//g

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM