How to remove ^M character with VI

This is how you remove those annoying ^M characters that show up in files previously edited on a Windows/DOS platform.

In VI,

actual command,

Here’s a walk through video I made. My first actually 🙂

  • Damn.. i was thinking of doing screencasts as well.
    Good job man. 🙂

  • Thanks sir, more to come. It’s fun 😀

  • hi, the ^M are showing for me on windows xp, i tried this command %s/^M//g but it doesn’t work, as soon as i put in the ctrl-m it says it cant find the pattern, is there some difference that i have to do to get to use this command on windows?

  • i found this to work on windows xp


    to replace all ^M with nothing in the whole file

  • Thanks for sharing the solution ciprian.

  • Thanks man, very helpful.

  • Eric

    Hi Ciprian, i am editing the file vi putty remoting from win 2008 SBS and the command %s/\r//g does’t seems to work either.

    Thanks in advance

  • Eric,

    You are remoting into a linux box or win xp box?
    If linux then pres %s/Ctrl-VCtrl-M//g
    The Ctrl-VCtrl-M should show up like a ^M
    If on windows i dont know of any other way besides %s/\r//g
    Let us know if you find out

    • Anjlica

      Ctrl-VCtrl-M works.

  • Kiran

    But I dont want to open the file using VI and then type at :

    Can i just do it without opening the file ?

  • lambzee

    Awesome you saved about 1 hr for me

    Thanks a lot

  • Brent

    You might consider just running dos2unix utility on the file.

  • sd


  • Lean

    How can you do that without using VI? I mean, I want to include that in a UNIX script.

  • Lean, do a dos2unix

  • RamyaChander

    the below command will clear the ^M character

© 2008-2012 The Danesh Project
Powered by Wordpress and made by Guerrilla. Hosted at ServerFreak