Thursday, January 8, 2009

Removing Ctrl-M

There are quite a few ways of doing this, but a quick way to remove ^M scattered around your file is as follows:

:%s/C-vC-m//g

Where C-v is Ctrl-v and C-m is Ctrl-m.

6 comments:

Anonymous said...

Another way:

:set ff=unix
:w

This will convert DOS text format into Unix text format.

Greg Whitescarver said...

Strangely, replacing a literal line break with another literal line break (ctrl-V enter) gets rid of them too:

:%s/^M/^M/g

Chris said...

For me;

:set ff=unix

does not work. I use the following..

function! CleanScript()
:%s/^M//g
:%s/^[//g
:%s/\[K//g
:%s/^H//g
endf

where ^M and the rest are entered via ctrl-v ctrl-m

graywh said...

Entering <C-v><C-m> is the same as using \r.

Replacing all \r's with \r actually changes the document is in :h sub-replace-special. Vim uses carriage returns internally to store a file's newlines regardless of the setting for 'fileformat' (which only matters at read/write). Actual newlines, \n, are used to represent NULs.

And changing 'ff' after the file is loaded only changes what is written for EOL when the file is saved. It will never remove those ^M's.

Doug Potts said...

I find that this works a little better, at least for me:

%s/^M\+$//

Which will get rid of carriage returns, of one or more in a row, at the end of the line.

This keeps it from removing the ^M in cases where you actually wanted it there, like the old style vi control-char mappings.

Cheers.

Anonymous said...

or u can use dos2unix

#dos2unix src.txt dest.txt