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.
- Navigate on over to one of the offending ’ characters.
- Press xu
- Type :%s/<c-r>"/'/g
- Repeat for all of the other smarts in the file.
Why xu rather than yl?
ReplyDeleteNo 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