how to get rid of ^M character using vi

Dan Welch welchdw at earthlink.net
Sun Jan 25 07:37:51 PST 2004


On Sun, Jan 25, 2004 at 09:43:21AM +0000, marlon corleone wrote:
> how do i get rid of this annoying character ^M using vi, in pico i used the 
> arguments '-w'
> but what about in vi?

This colon (ed) command works in FreeBSD's included vi's
command mode:

:%s/^M//g

followed by pressing Enter.  The ^M (or Ctrl-M) typed to
show it here should actually be entered by pressing ^v
^M in sequence. (The ^v preface tells vi to use the next
keystroke literally instead of taking it as a command.)

In a slight variation  you can take advantage the
replacement field in the substitution (which I left blank)
to produce a blank line between the original lines --
useful when the ^M marks the paragraph breaks:

:%s/^M/^M/g

It looks redundant, but after playing with the first version
you'll probably see what's going on.

You may also find the following short-cut useful for simpler
files:
:%s/^M
i.e., press Enter just after getting the ^M on the command
line, thus ending prematurely.  This works quite usefully
when the file has only one ^M per line and none embedded.



More information about the freebsd-questions mailing list