149

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?

Henke
  • 4,445
  • 3
  • 31
  • 44
DHamrick
  • 8,338
  • 9
  • 45
  • 62
  • 19
    It'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
  • 15
    There'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
  • 3
    possible 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 Answers9

137

As a command, type

:%s/^M$//

(To get ^M, press ^V ^M, where ^ is CTRL on most keyboards)

Tobias Baaz
  • 1,840
  • 1
  • 12
  • 8
  • 73
    also, :%s/\r$// to avoid the ^V^M hassle – glenn jackman Jul 11 '09 at 13:53
  • 10
    Or, saving a character: `:%s/\r$` – sehe Dec 05 '11 at 12:14
  • 2
    @sehe technically saves 2 characters :) – Chris Sep 07 '12 at 22:42
  • @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
100

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
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 11
    vim 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
  • 3
    Vim 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
  • Tommy, that's a limitation on `diff`, not on `git`. – Spidey Oct 27 '17 at 16:36
35

You can do this:

:set fileformats=dos

It will hide the ^M's, without touching the file.

jqno
  • 15,133
  • 7
  • 57
  • 84
19

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

Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
7

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
ib.
  • 27,830
  • 11
  • 80
  • 100
TheRealKingK
  • 829
  • 7
  • 14
2

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

T Percival
  • 8,526
  • 3
  • 43
  • 43
1

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

Community
  • 1
  • 1
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
0

mcedit: shift+f2, set unix format (LF), ok

0
" 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()
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40