Tuesday, January 17, 2012

You've Got This Big Z In Your Code


Nasty

We've all seen it at one time or another. A big, nasty, hairy, cranky looking THING in our nice, clean, precious text file. How the hell did that get there?! More importantly... How do I get rid of it?

Diagnosis

This condition can present itself in several ways, like the unprintable character code in angle brackets:

 F<83>k me

...an intimidatingly visible character from another land:

 好天

...or just fancy versions of good old regular ASCII characters:

 I’m sick of ‘smart’ quotes


Complications


Navigating through your whole file looking for and exterminating these little warts by hand would be tedious and we're Vimmers after all, so we should be thinking in sharper tools, like... a whole buffer substitution. Great. Now we have a new problem... How do I type one of those eyesores into the substitute command...? We'll use a little dash of Register Pasting magic.

Caveat

This could be an encoding problem, so make sure   :set enc? fenc?   return sane values before you go stripping out or replacing these crusty critters.

Cure

Let's take the smart quotes example and clean them up to be ordinary, normal, sane, plain quotes. We'll tackle that smart single quote case from above.

  1. Navigate on over to one of the offending ’ characters.
  2. Press xu
  3. Type :%s/<c-r>"/'/g
  4. Repeat for all of the other smarts in the file.
Hopefully when you next break out in an embarrassing rash of wicked wigglies, frightful foreigns or undecorous unprintables you will now be able to get righteous on their bad selves.

2 comments:

  1. Replies
    1. No good reason at all. Both are fine here. I guess I'm more of a destructionist and reach for my x quicker than y. ;)

      Delete