^M at the end of every line in Vim
Asked Answered
V

11

151

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?

Vachill answered 10/7, 2009 at 16:48 Comment(4)
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.Satanic
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 ^Ms. 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.Phenacaine
possible duplicate of Convert DOS line endings to Linux line endings in vimRuthi
Does this answer your question? Convert ^M (Windows) line breaks to normal line breaksDeanadeanda
V
139

As a command, type

:%s/^M$//

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

Vi answered 10/7, 2009 at 16:51 Comment(4)
also, :%s/\r$// to avoid the ^V^M hassleDematerialize
Or, saving a character: :%s/\r$Joo
@Joo technically saves 2 characters :)Belaud
@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).Riproaring
S
103

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
Shulins answered 5/12, 2011 at 12:5 Comment(4)
vim wasn't finding any ^m, or \r, or any of the other line endings, but this worked. Thanks :)Demetria
Vim would not find any ^M or \r, but this did work for me! Thanks @ShulinsEster
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 :/Chastitychasuble
Tommy, that's a limitation on diff, not on git.Surreptitious
S
35

You can do this:

:set fileformats=dos

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

Skuld answered 10/7, 2009 at 22:16 Comment(0)
J
19

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

Jolley answered 10/7, 2009 at 16:51 Comment(0)
U
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
Unionist answered 31/10, 2011 at 6:21 Comment(1)
Thanks, wondered how to actually enter Ctrl-M in search-replace!Campney
S
2

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

Shrier answered 10/7, 2009 at 22:10 Comment(0)
M
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."

Myke answered 10/7, 2009 at 16:52 Comment(0)
S
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()
Strati answered 31/10, 2011 at 10:48 Comment(0)
J
0

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

Jadwigajae answered 17/12, 2014 at 8:18 Comment(0)
I
0

My situation is, I was developing my project on Windows, later I ran a Linux virtual machine, and by sharing folders, I directly opened the project that was originally on Windows in the Linux virtual machine. When I ran git status, a large number of files were listed, and each line of these files ended with ^M. I solved this issue with the following two commands:

git status -s | grep "^ M" | awk '{print $2}' | xargs dos2unix

git status -s | grep "^ M" | awk '{print $2}' | xargs git add

Immigrate answered 18/2, 2024 at 7:50 Comment(0)
I
0
:%s/\r$//

Should remove all carriage returns \r (displayed as ^M) at end of lines.

Italian answered 22/3, 2024 at 10:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.