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?

2

10 Answers 10

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

4
  • 3
    I usually just whack 'em with :%s/^M//
    – TMN
    Jul 26, 2011 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. Jul 10, 2012 at 2: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. May 3, 2013 at 22:51
  • See also the section "Converting mixed files" in this comprehensive explanation of vim line endings vim.wikia.com/wiki/File_format
    – axiopisty
    Aug 14, 2013 at 20:30
45

You can also run:

:e ++ff=dos

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

2
  • 4
    This was what I was looking for, and is the actual correct answer. Thanks.
    – Jay Taylor
    Dec 9, 2011 at 1:43
  • This works well also from the command line: vim -R -c ":e ++ff=dos" filename
    – Kafumanto
    Nov 22, 2023 at 10:58
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.

1
  • 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, 2009 at 6: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.

0
6

You can ignore these chars!

put this into your vimrc

match Ignore /\r$/

3
  • 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, 2013 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, 2014 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, 2017 at 5:07
5

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.

4

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

:set ffs=dos

not just: ff

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.

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.