« Building a Fort in the Living Room | Main | Least Deadly State for Driving: MA »

August 27, 2004

Changing end-of-line Mode in Emacs (replaces ^M line return)

Every now and then I run into this. I export CSV data from Excel and find that when trying to use it in other places the line-returns aren't quite right and unix tools like 'less' and 'pico' think the file is one long line, with ^M in places where a line-return should be.

Wanting to replace the ^M I open the file in emacs, which is smart enough to go into Mac mode and show the file with the correct line returns.

I poked around and found that if the ^M is showing up in emacs this sequence will replace them:
1. M-% (esc % on Mac X11 Emacs)
2. ctrl-q ctrl-m RET (specifies ^M to replace)
3. ctrl-q ctrl-j RET (specifies a line return as the replacement)
4. ! (runs the replace on the entire file)

But in my case the ^M isn't showing because emacs knows to use ^M for a line return on mac. So what I need to do is change the end-of-line, which is specified in buffer-file-coding-system. You can change the file from Mac mode to unix with this command:

M-x set-buffer-file-coding-system RET unix

When you do that emacs suggests using this shortcut:

ctrl-x RET f unix RET

Save the file after changing the end-of-line and all is good in my other unix utilities.

Posted by mike at August 27, 2004 10:34 AM