82

I'm using gVim on Windows. My code shows ^M characters at the end of lines. I used :set ff=dos to no avail. The ^M characters remain for existing lines, but don't show up for newlines I enter. I've switched modes to mac (shows ^J characters) and unix (also shows ^M characters) and back to dos. Has anyone else seen this?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Jerph
  • 4,572
  • 3
  • 42
  • 41
  • See http://vim.wikia.com/wiki/File_format for a comprehensive explanation of line endings in vim/gvim. This link also explains how to fix the problem. – axiopisty Aug 14 '13 at 20:33
  • This question is _not_ a duplicate of any of the following three questions, but it deserves to be linked with them, I think. https://stackoverflow.com/q/5843495 ~ * ~ https://stackoverflow.com/q/811193 ~ * ~ https://stackoverflow.com/q/82726 – Henke Oct 26 '22 at 18:34

10 Answers10

144

This happens when you have a mixture of Windows line endings and Unix ones. If you have 100 lines, 99 are \r\n and one is \n, you'll see 99 ^M characters. The fix is to find that one line and replace it. Or run dos2unix on the file. You can replace the Windows line endings with:

:%s/\r\(\n\)/\1/g

laurent
  • 88,262
  • 77
  • 290
  • 428
richq
  • 55,548
  • 20
  • 150
  • 144
  • 3
    I usually just whack 'em with `:%s/^M//` – TMN Jul 26 '11 at 14:43
  • 1
    Should add to `:set ff=dos` if you want it to be dos when you're done, ala Evan's answer. – Merlyn Morgan-Graham Jul 10 '12 at 02:20
  • Excelent solution! To be more practial in next times add in the .vimrc file: `command AdjustEndOfLine execute '%s/\r\(\n\)/\1/g'` So, every time you need to sanitize the file, just use the `AdjustEndOfLine` command. – Tárcio Zemel May 03 '13 at 22:51
  • See also the section "Converting mixed files" in this comprehensive explanation of vim line endings http://vim.wikia.com/wiki/File_format – axiopisty Aug 14 '13 at 20:30
44

You can also run:

:e ++ff=dos

To remove the ^M: See File format – Vim Tips Wiki.

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
beckelmw
  • 1,722
  • 2
  • 16
  • 23
19

I usually use the following to cleanup my line endings:

:g/^M$/s///

To get the ctrl-M I usually type ctrl-Q, then ctrl-M and it puts it in. (In some environments it may be ctrl-V then ctrl-M.) I don't know why, but I find that one easier to remember than rq's.

Don't forget to do :set ff=dos as well, or you'll end up saving with UNIX line endings still.

Community
  • 1
  • 1
Evan
  • 18,183
  • 8
  • 41
  • 48
  • Yeah, I normally use the ^M version with Ctrl-Q and all that. But it's tougher to explain ;-) and the group match version is copy paste friendly. – richq Apr 29 '09 at 06:07
15

I know this has already been answered, but a trick I use is

:%s/\r/\r/g

This replaces the unix carriage returns with the windows CRLF. Just added in case anyone else had issues.

dsrekab
  • 456
  • 4
  • 13
6

You can ignore these chars!

put this into your vimrc

match Ignore /\r$/

IvanM
  • 2,913
  • 2
  • 30
  • 30
  • 1
    Good to know this is there, but is there a good use case for leaving mismatched line endings in a file? Seems like it's asking for trouble. – Jerph Mar 24 '13 at 13:44
  • 1
    This is more what I needed (changing the file is not always advisable when it causes massive changes to a file that show up in some revision control systems) – RunHolt Mar 12 '14 at 10:51
  • Unfortunately this changes line endings as soon as you save the file. So it produces exactly these unwanted big changes in whitespace, only without showing you. At least for me. :( – peschü Mar 21 '17 at 05:07
4

Actually what worked for me (on 64-bit windows, gVIM: 7.2 ) was:

:set ffs=dos

not just: ff

ra170
  • 3,643
  • 7
  • 38
  • 52
4

Running Vim 7.3 on Windows 7. I used the following command:

:%s/^M/\r/g

To create the ^M I typed in CTRL+Q then CTRL+M.

ddelrio1986
  • 279
  • 1
  • 3
  • 15
2

This is probably a bit simple for many of you but on the off chance it's useful.

Based on richq's answer, I found these to be useful in my vimrc. Note, the second one is commented out normally because it makes dd a bit confusing since Vim will wait for another key stroke to work out if it's the mapped ex command.

function! D2u()
    execute '%s/\r\(\n\)/\1/g'
endfunction
"map d2u :%s/\r\(\n\)/\1/g

The first is run by typing call D2u() into ex and the second by pressing D2u in edit mode.

Peter Shannon
  • 191
  • 1
  • 3
2

These are extra CR line endings usually because of a using a file on mixed UNIX/DOS systems.

Possible the shortest answer to remove a single ^M from the end of each line, and what I use, is:

:%s/\r

which is equivalent to:

:%s/\r//

but the end slashes aren't required (they're assumed).

thebriguy
  • 183
  • 2
  • 7
2

tried a lot of things but the following worked

:%s/\r/\r/g

note: use g if you want the effect on the whole file

Atul Subhash
  • 53
  • 1
  • 6