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

前端 未结 10 1409
小蘑菇
小蘑菇 2020-12-22 15:25

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

相关标签:
10条回答
  • 2020-12-22 16:12

    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.

    0 讨论(0)
  • 2020-12-22 16:13

    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 讨论(0)
  • 2020-12-22 16:14

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

    0 讨论(0)
  • 2020-12-22 16:15

    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

    0 讨论(0)
提交回复
热议问题