How to remove ^M from unix files using VI editor

^M is DOS line break charater which shows up in unix files when uploaded from a windows file system in ascii format.

To remove this, open your file in vi editor and type

:%s/(ctrl-v)(ctrl-m)//g

and press Enter key.

Important!! – press (Ctrl-v) (Ctrl-m) combination to enter ^M character, dont use “^” and M.

If anything goes wrong exit with q!.

I learned this from here.

41 thoughts on “How to remove ^M from unix files using VI editor

  1. Your substitution command may catch more ^M then necessary. Your file may contain valid ^M in the middle of a line of code for example. Use the following command instead to remove only those at the very end of lines:
    :%s/(ctrl-v)(ctrl-m)*$//g

    In some flavors of vi (e.g. RedHat), vi does it for you with:
    :set fileformat=unix

  2. Thanks guys…this worked for me to remove ^M from my input file. But any idea why this comes when we ftp the a file from windows to UNIX box on ASCII mode?

  3. I was really pissed off with this ^M in my file,
    thanks to Scott i use
    :set fileformat=unix
    and it works……

  4. Pingback: Removendo o caracter ^M com o VIM | ATALIBA TEIXEIRA

  5. fyuhhhh.. thanks God I found this link..
    I need to overtime until 4.00 to find the root cause, and it’s because stubborn ^M…. damn 🙂

    • to send from windows, copy it into notepad first, or use the ‘Paste As’ command, and select plain text. (I dont think you can ‘paste as’ directly into notepad)

  6. You can definitely see your enthusiasm within the article you write.
    The arena hopes for even more passionate writers such as you who aren’t afraid to mention how they believe. All the time go after your heart.

  7. Thanks so much for sharing all with the awesome info! I am looking forward to checking out far more posts! dccfacefkdbg

  8. Pingback: Borrar retornos de carro con vi | tikismikis.org

Leave a reply to herry Cancel reply