Thursday, August 18, 2005

Dos^M return^M character^M hell.^M^M

Of poking fish with a stick and carriage returns.
Vim editorMust there be 20 different standards for everything? I can understand how many languages, dialects, systems of weights and measures, etc. spring up in the world. They were created in isolation over the centuries. A group of people on one side of the globe, poking at fish with a stick for thousands of years are naturally going to develop their language and customs in different ways than that group on the other side who have to smash through an ice sheet if they want to poke a fish with a stick. But in this era of the world wide web, where we are all poking at fish in the same river, why the heck do we have to do it so incompatibly? Why not just agree on one way so we can get down to the good part, which is bragging who pokes a fish the best?

Before you ask me if I would rather we all spoke one language and had the same customs I should say that I think diversity is a good thing, but I am not talking about diversity. I'm talking about carriage returns. I don't think there is any added value in having many different ways of ending a line of text. Sure, sure, it is all a rich tapestry. But who cares?! I don't care how my line ends, as long as it begins down on the next one. My side of the world is the unix side. I poke my fish with a stick called vim. But, occasionally I wander and find myself among those who poke fish differently, at which point life becomes a bit irksome because of the carriage return. I open a file and it is all on one line and riddled with ^M's! What the heck is that? In windows a line ends with ^M (a carriage return followed by a line feed), in unix it is just a line feed, Mac uses just a carriage return. Fortunately, those of us who use a vim stick can cope, although it is not obvious how it is done (I'm getting there, bear with me). Historians may have something to say about my explanation for all this diversity, but I am going to theorize that eons ago there was a mythic clan of geeks led by Bill Gates. They were known for doing everything backwards, wearing their socks over their boots, and ending lines of text with both a carriage return and a line feed. (They obviously didn't know what either one did and thought "what the heck, throw them both in.") However, they happened to be a very shrewd clan who knew how to convince others that in fact socks were meant to go over boots. And then they took over the world. Well, it could have happened that way. Anyway, after all that pointless rambling I am going to leave you with one useful nugget. How to use your vim stick to poke fish in any part of the world you might find yourself.

nugget
To find and replace all ^M characters in a file with unix line feeds do this:[code] :%s/^M/\r/g[/code] Make sure you type the ^M as "CTRL-V CTRL-M" NOT "CARROT M"! Written in english that command says :%s find and replace /^M/ carriage return line feed \r/ with line feed g globally (not just the first instance). Not intuitive, but who ever said vim was intuitive? I just hope this can help you get back to doing what you do no matter what part of the world you find yourself. **pokes fish, smiles** Yep. Life is good.

References:
http://www.vim.org/tips/tip.php?tip_id=26
http://en.wikipedia.org/wiki/Carriage_return

No comments: