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

[youtube]http://www.youtube.com/watch?v=Fy_w3VDkxKU[/youtube]

8 Responses to “How to remove ^M character with VI”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Raja says:

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

  2. Danny says:

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

  3. ciprian says:

    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?

  4. ciprian says:

    i found this to work on windows xp

    %s/\r//g

    to replace all ^M with nothing in the whole file

  5. Danny says:

    Thanks for sharing the solution ciprian.

  6. Thanks man, very helpful.

  7. Eric says:

    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

  8. ciprian says:

    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

Leave a Reply

© 2008-2009 The Danesh Project
Powered by Wordpress and made by Guerrilla. Best viewed in Mozilla Firefox