When I am editing source files using Vim and other editors, sometimes I get these ^M
characters at the end of each line.
I think that it has something to do with editing a file on Windows and then on Linux.
How can I remove all of these automatically?
-
19It's because Windows uses a two-character sequence (normally written "\r\n") to represent a line break, but UNIX/Linux uses only the second character "\n" to represent a line break. So when you edit a Windows text file on a Linux editor, the editor sees extra characters that it doesn't consider part of the line breaks, so it tries to render them and what comes out is ^M. – David Z Jul 10 '09 at 16:57
-
15There's a bit more to it than that, David. Vim will happily edit a text file with DOS line endings without showing all those `^M`s. The only indication you have when editing a DOS text file in Vim is if you have `%{&ff}` in your `statusline` option value. Vim shows `^M` when the line ending style is *mixed*. It means you've used a text editor that isn't as savvy as Vim, which hasn't followed the existing line ending style, as Vim will by default. If you use Vim or something else of its calibre on Windows, you won't get wrecked line endings like that. – Warren Young Nov 02 '11 at 09:28
-
3possible duplicate of [Convert DOS line endings to Linux line endings in vim](http://stackoverflow.com/questions/82726/convert-dos-line-endings-to-linux-line-endings-in-vim) – cfi Sep 11 '15 at 11:26
-
Does this answer your question? [Convert ^M (Windows) line breaks to normal line breaks](https://stackoverflow.com/q/811193) – Henke Oct 27 '22 at 15:29
9 Answers
As a command, type
:%s/^M$//
(To get ^M, press ^V ^M, where ^ is CTRL on most keyboards)

- 1,840
- 1
- 12
- 8
-
73
-
10
-
2
-
@glennjackman, your tip was much easier, as I had been struggling to get the ^V + ^M were it was not happening at all. All I was getting was the content in the clipboard (as ^V was effective every time ,despite my repeated attempts with different strategies to get them both). – itsraghz Mar 24 '20 at 03:01
One easy way to strip out the DOS line endings is to use the ff
option:
:set ff=unix
:wq
Now your file is back to the good-old-Unix-way.
If you want to add the DOS line-endings (to keep a printer happy, or transfer files with Windows friends who don't have nice tools) you can go the opposite direction easily:
:set ff=dos
:wq

- 102,305
- 22
- 181
- 238
-
11vim wasn't finding any ^m, or \r, or any of the other line endings, but this worked. Thanks :) – SgtPooki Apr 11 '14 at 16:22
-
3Vim would not find any ^M or \r, but this did work for me! Thanks @sarnold – 7ochem Feb 10 '15 at 10:54
-
it would seem git is too dumb to detect this as a single character change. I ran this in Vim and the `git diff` shows every line deleted and every line added :/ – Tommy Apr 18 '17 at 20:37
-
You can do this:
:set fileformats=dos
It will hide the ^M
's, without touching the file.

- 15,133
- 7
- 57
- 84
There's a program called dos2unix that should strip those for you. Windows uses different line-ending characters which is why that happens.

- 35,156
- 14
- 91
- 107
This worked for me in a file that had everything on one line:
First find all matches
:%s/^M//
(To get ^M
, press ^V ^M, where ^ is Ctrl on most keyboards)
Then replace with newlines
:%s//\r/g
Combined command would be:
:%s/^M/\r/g

- 27,830
- 11
- 80
- 100

- 829
- 7
- 14
I tend to run afflicted files through fromdos
before reopening them. fromdos
is part of the tofrodos package.

- 8,526
- 3
- 43
- 43
The origin of the problem may have been through an FTP transfer. When you FTP these files from one box to another, make sure to use ASCII transfers. Use the command "ASC."

- 1
- 1

- 34,865
- 12
- 85
- 147
" put this in your ~/.vimrc file and :source ~/.vimrc
" then you can do: Dos2Unix
" dos2unix ^M
fun! Dos2unixFunction()
let _s=@/
let l = line(".")
let c = col(".")
try
set ff=unix
w!
"%s/\%x0d$//e
catch /E32:/
echo "Sorry, first save the file."
endtry
let @/=_s
call cursor(l, c)
endfun
com! Dos2Unix keepjumps call Dos2unixFunction()

- 11,069
- 3
- 50
- 40