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,

:%s/[ctrlkey+v and ctrl-key+M]//g

actual command,

:%s/^V^M//g

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

  • http://rajaseelan.com Raja

    Damn.. i was thinking of doing screencasts as well.
    Good job man. :)

  • http://thedaneshproject.com Danny

    Thanks sir, more to come. It’s fun :D

  • http://www.fotobleu.com/ ciprian

    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?

  • http://www.fotobleu.com/ ciprian

    i found this to work on windows xp

    %s/\r//g

    to replace all ^M with nothing in the whole file

  • http://thedaneshproject.com Danny

    Thanks for sharing the solution ciprian.

  • http://design.liberta.co.za Liberta Design

    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

  • http://www.fotoblogist.com ciprian

    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
    Thanks

    • Anjlica

      Ctrl-VCtrl-M works.
      Thanks.

  • 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

    thanks

  • Lean

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

  • http://thedaneshproject.com Danesh

    Lean, do a dos2unix

  • RamyaChander

    the below command will clear the ^M character
    :%s/[ctrlkey+v+M]//g

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