Tuesday, May 3, 2011

gVim showing carriage return (^M) even when file mode is explicitly DOS

I really don't want to give up on vim again, but every time I try to learn it something gets in the way.

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?

From stackoverflow
  • 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.

    Jerph : I found that one line just before you wrote this. :) Thanks!
    rq : Heh, typical. Vim is great, so stick with it! :-)
    espais : i wish i could give you +1000 for this
  • 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.

    rq : 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.
  • Neither find/replace worked in my case. gvim complained it couldn't find . All I have is ^M$ all i have is one line line with ^M's all over the place. I deleted and replaced the end of the line but That didn't help either. I guess I could replace all the ^Ms with line breaks.

  • 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 comments:

Post a Comment

Note: Only a member of this blog may post a comment.