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

0 votes
asked May 1, 2009 by bytenirvana

vim shows on every line ending ^M

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

30 Answers

0 votes
answered May 1, 2009 by paul-tomblin

On Linux and Mac OS, the following works,


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.

0 votes
answered May 1, 2009 by andrew-sledge

^M is retrieved by Ctrl+V and M, so do

0 votes
answered May 1, 2009 by leopardskinpillboxha



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



substitute, % = all lines


^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)


with new line (\r)


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

0 votes
answered May 1, 2009 by jonathan-leffler

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.

0 votes
answered May 1, 2009 by concernedoftunbridge

Alternatively, there are open-source utilities called dos2unix and unix2dos available that do this very thing. On a linux system they are probably installed by default; for a windows system you can download them from amongst others.

0 votes
answered Jan 25, 2011 by user2571881

" This function preserves the list of jumps

fun! Dos2unixFunction()
let _s=@/
let l = line(".")
let c = col(".")
    set ff=unix
catch /E32:/
    echo "Sorry, the file is not saved."
let @/=_s
call cursor(l, c)
com! Dos2Unix keepjumps call Dos2unixFunction()
0 votes
answered May 25, 2011 by metagrapher

in order to get the ^M character to match I had to visually select it and then use the OS copy to clipboard command to retrieve it. You can test it by doing a search for the character before trying the replace command.


should select the first bad line


will replace all the errant ^M with carriage returns.

This is as functions in MacVim, which is based on gvim 7.


Having this problem again on my Windows 10 machine, which has Ubuntu for Windows, and I think this is causing fileformat issues for vim. In this case changing the ff to unix, mac, or dos did nothing other than to change the ^M to ^J and back again.

The solution in this case:

:%s/\r$/ /g
:%s/ $//g

The reason I went this route is because I wanted to ensure I was being non-destructive with my file. I could have :%s/\r$//g but that would have deleted the carriage returns right out, and could have had unexpected results. Instead we convert the singular CR character, here a ^M character, into a space, and then remove all spaces at the end of lines (which for me is a desirable result regardless)

Sorry for reviving an old question that has long since been answered, but there seemed to be some confusion afoot and I thought I'd help clear some of that up since this is coming up high in google searches.

0 votes
answered Jan 25, 2012 by ramesh

use dos2unix utility if the file was created on windows, use mac2unix utility if the file was created on mac. :)

0 votes
answered May 7, 2012 by chenxsan

First, use :set ff? to figure out the file format your file is.

I guess it could be unix, then the problem is your file was created with fileformat=dos adding "^M^J" to the line end but read with flieformat=unix only removing the "^J" from the line end, leaving the "^M" there.

Just input :e ++ff=dos in Vim command line to change your file's format from unix to dos. It should solve the problem. If not, :%s/\r//g should help you out.

0 votes
answered May 27, 2012 by netpoetica

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:


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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter