how to make vim show ^M and substitute it

Viewed 111465

my vim show tab as --->, but does not show windows ^M character.

And , how substitute it in vim.

renew ============

I check my vimrc it is set fileformat=unix but when I open a dos file set ff is dos

6 Answers

To remove ^M characters from you vim: In command mode type

%s/

followed by

ctrl+v and Enter. 

It should then look like :

%s/^M

Lastly replace with blank character :

%s/^M//g

Old fashioned way - works even in vi:

    vim -b filename
    :%s/^V^M//g
    :x

Explanations:

  • vim -b opens vim in binary mode, all control characters will be visible
  • vi doesn't have -b option but then again vi doesn't need it, it will show ^M by default
  • :%s is search replace - we are replacing ^M with nothing
  • You have to type Control-V Control-M in a sequence, you can keep Control down all the time, or you can release it and press again - both works.
  • ^V will disappear, only ^M will stay. ^V means insert next character literally (try pressing Escape after ^V). Without it Control-M would just mean Carriage Return - or same as pressing Enter.
  • Don't try to copy ^M with a mouse, it will break it into two characters ^ and M and it will mean search for M at the beginning of the line.
  • g means global - if you have ^M only on end of line you don't really need it
  • :x means save and exit - almost no one is using it
Related