42

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 character in VIM (to use in commnad-line mode).

igorgue
  • 16,292
  • 12
  • 35
  • 52
  • The general "replace by newline" question can be found at: http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim But since finding carriage return is a bit special because of `set ff`, and this is the first Google hit, maybe we should keep this separate. – Ciro Santilli新疆棉花TRUMP BAN BAD Nov 13 '14 at 08:01

6 Answers6

62

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.

ZyX
  • 49,123
  • 7
  • 101
  • 128
  • 2
    great; I read [this](http://stackoverflow.com/questions/71417/why-is-r-a-newline-for-vim) to better understand `\n` and `\r` when searching and replacing with vim – Abraham Sangha Nov 29 '16 at 21:23
  • if you are running this in vim script then it is `exec "%s/\r/\r/g"` – phyatt Dec 19 '16 at 19:59
44

:%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
16

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>.

mike3996
  • 15,564
  • 6
  • 62
  • 77
11

In vim session try:

:%s/^M//g

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

anubhava
  • 664,788
  • 59
  • 469
  • 547
  • 3
    Technically it is two separate keystrokes. Anything after a `` will be inserted literally. – mike3996 May 09 '11 at 15:54
  • Thanks, but I knew how to do that, for one odd reason this file only includes `^M` no return after that, so I had to do `:%s/^M/^M/g` where the last `^M` is the actual return. – igorgue May 09 '11 at 15:54
9

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
ZyX
  • 49,123
  • 7
  • 101
  • 128
Robin
  • 479
  • 6
  • 12
0

LF: Line Feed

CRLF: Carriage Return and Line Feed

^M : What vim interpretes Carriage Return part as. You can also search it with \r

For my case I wanted not to replace it with any other format of line break (LF or etc.) but just period and whitespace (. ). Because what supposed to be was a new sentence but somehow carriage return involved in. Probably user typo.

From

I'll be there^M
We can talk.

To

I'll be there. We can talk.

What helped me is:

:%s/^M\n/\. 

or

:%s/\r\n/\. 

(there's a whitespace in the end)

Both worked fine for me. \r\n easier to type.

Ctrl+M , Ctrl+V to insert ^M

(CentOS Linux 7.2)

Sedat Kestepe
  • 75
  • 2
  • 8