Edit file on Windows, shows ^M on each line on Linux

Paul Seamons paul at seamons.com
Fri Apr 13 10:57:32 MDT 2007

> You can use dos2unix to stip the ^M from your files in Unix and Linux:
> $ dos2unix filename.ext

For all of you emacs fans out there (I know there has to be at least one) if 
you open your file and see the lovely '(DOS)' thing on your mode-line you can 
use the following to get rid of the dos endings:

C-x RET c
unix RET
C-x C-v RET

M-% C-q C-m RET RET !
C-x C-s

Wow - that is a lot of typing.  Well - it actually isn't that bad and it 
allows you to be in more control of what is going on and keep an eye on 
things the entire time.  I'll explain more detailed later - but the first 
three lines are getting the ^M letters to show.  The last two remove the 
lines and save your file.

I'm sure all of the vi guys just had soda-pop come out their nose when they 
saw the list of emacs commands.  So now that you've laughed, think about how 
you'll do this in vi.  Well the first three lines of the emacs code are there 
because emacs is allowing you to edit the file and maintain the line 
endings - Vim by default shows you the ^M characters and makes you type 
additional ones if you want to keep the same line endings.

Well the direct comparison part is the search replace - here is how you'd do 
it in Vim:

:%s/ C-q C-m //gc RET a

Yup that is a lot shorter isn't it.  I know you could get rid of the c on the 
swap and then you wouldn't need the a, but it is nice to be interactive of a 
moment to make sure you are deleting things you down want to.  Sure looks 
like both Vim and Emacs are powerful - but really non-intuitive.  I know you 
could also pipe the region out to a separate process and replace the contents 
with the output of the separate process - but how many of you actually have 
dos2unix installed by default.  All of you IDE guys don't have a leg to stand 
on in comparison to emacs or vim either.  Try and do this same operation from 
the keyboard and you'll have something like:

C-r (figure out how to put in a ^M here) TAB TAB SPACE (to check the 
replace_all box) TAB TAB RET (to start the dialog)
Alt-f s (to save the file)

I know you could use the mouse.  But real men don't use mice :)

Well I started out trying to give a simple explanation of how to do this in 
emacs and now I should have successfully offended the IDE crowd, the Vim 
crowd, and probably the emacs crowd, and probably the notepad.exe crowd 
because i didn't mention how to do it in notepad.

Well - such are mailing-lists.

So here is the break down of the emacs commands:

1) C-x RET c

That is Control-x then the Enter (RETURN) key then the c key.  This is the 
same as typing "M-x universal-coding-system-argument".  Emacs is asking what 
coding system you want to apply on a following argument.

You'll see a prompt that looks like this:

Coding system for following command:

2) unix RET

That is typing "unix" and then the Enter (RETURN) key.  This says we want to 
use unix coding for the following command.

3) C-x C-v RET

C-x C-v is the same as typing "M-x find-alternate-file" which when you just 
hit RET immediately reloads the current file.

4) The M-% starts search and replace (same as "M-x query-replace").  The C-q 
allows you to insert control characters.  We then enter the C-m which is the 
^M control character (same way as vim enters this character).  The query 
replace is interactive by default.  The ! (same as the "a" in vim) replaces 
the remaining instances.

5) The C-x C-s saves the file.

The funny thing about this reply to me is it shows how absurd emacs vs vim vs 
IDE wars are (even though I probably just started one).  Emacs and Vim in 
their current incarnations (if software can be incarnate) can do as much as 
each other and usually take just as much typing to do so.  But both of them 
are so obscure that it would be impossible for a newbie to try and get any 
real work done in either without a basic primer.  IDEs aren't too much better 
but at least the user could hunt and peck for the options.


More information about the PLUG mailing list