How to convert the ^M linebreak to 'normal' linebreak in a file opened in vim?

vim shows on every line ending ^M

How I do to replace this with a 'normal' linebreak?


Solution 1:

Command

:%s/<Ctrl-V><Ctrl-M>/\r/g

Where <Ctrl-V><Ctrl-M> means type Ctrl+V then Ctrl+M.

Explanation

:%s

substitute, % = all lines

<Ctrl-V><Ctrl-M>

^M characters (the Ctrl-V is a Vim way of writing the Ctrl ^ character and Ctrl-M writes the M after the regular expression, resulting to ^M special character)

/\r/

with new line (\r)

g

And do it globally (not just the first occurrence on the line).

Solution 2:

On Linux and Mac OS, the following works,

:%s/^V^M/^V^M/g

where ^V^M means type Ctrl+V, then Ctrl+M.

Note: on Windows you probably want to use ^Q instead of ^V, since by default ^V is mapped to paste text.

Solution 3:

Within vim, look at the file format — DOS or Unix:

:set filetype=unix

:set fileformat=unix

The file will be written back without carriage return (CR, ^M) characters.

Solution 4:

This is the only thing that worked for me:

:e ++ff=dos

Found it at: http://vim.wikia.com/wiki/File_format

Solution 5:

A file I had created with BBEdit seen in MacVim was displaying a bunch of ^M line returns instead of regular ones. The following string replace solved the issue - hope this helps:

:%s/\r/\r/g

It's interesting because I'm replacing line breaks with the same character, but I suppose Vim just needs to get a fresh \r to display correctly. I'd be interested to know the underlying mechanics of why this works.