^M at the end of every line in vim

前端 未结 9 1885
独厮守ぢ
独厮守ぢ 2020-11-29 17:37

When I am editing source files using vim and other editors sometimes at the end of the line I get these ^M characters at the end of each line. I think that it has something

相关标签:
9条回答
  • 2020-11-29 18:11

    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
    
    0 讨论(0)
  • 2020-11-29 18:17

    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
    
    0 讨论(0)
  • 2020-11-29 18:23

    As a command, type

    :%s/^M$//
    

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

    0 讨论(0)
  • 2020-11-29 18:23

    You can do this:

    :set fileformats=dos
    

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

    0 讨论(0)
  • 2020-11-29 18:26

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

    0 讨论(0)
  • 2020-11-29 18:31

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

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