Posts Tagged ‘ Mac

Eliminating those pesky ^M’s in Vim

One on my pet peeves is dealing with a file using the 0x0D end-of-line (EOL) marquer. Such is the legacy of the Mac standard. Unix variants use 0x0A generally but even though Mac OS X is based on Unix, its seems that software used in that environment continues to produce the same old format.

But I’ve seen changes recently: one of my friends, an avid Mac user, updated a Web site lately, all pages with the correct EOL character … or at least his software does; I’m not sure if he noticed the difference anyways.

<tone down>Shhhh. He still uses ISO-8859-1 encoding</tone down>

Still, there are plenty of legacy files out there and it kills me when I have to edit one of those with my favorite editor, vim, and see all the lines squeezed into one big mess, intercalated with ^M‘s .

It kills me because there is a simple trick to correct this … and I always forget it. Aaaaarrrrgh!

This is why blogs exist: to keep your own tips on-line so you can check them out once in a while (and if other people check them out too, that’s also a Good Thing ™).

Here’s vim’s quick shortcut to stop this nonsense: enter the usual colon (:) to enable the command mode and enter any typical search-and-replace command using {Ctrl-V}{Ctrl-M} as the search pattern. Replace with a newline code such as \r, or nothing if you suspect the ^M’s are doubled with extra line feeds, like so:


What you’ll actually see is this:


There. I feel much better now.

Now, don’t get me started on Microsoft’s 0x0A 0x0D sequence. 🙂 Vim always dealt with it seamlessly as its “converted” mode. You can’t see the difference as you edit such file but you still output the same garbage once you save it. I wonder why no one thought about a similar filter for the 0x0D case. But my guess is that it will slowly phase out … eventually.

return top