Replacing those pesky smart quotes in VIM

Recently I’ve been running into a lot of silliness that appears in files exported from MS applications (Word, Excel, etc.) called ‘Smart quotes’.

Basically MS uses higher level ascii characters to represent quotes that mean more than regular quotes (whatever for?!). You see this weirdness in vi as <93><92> etc. which are the hex values of these characters. I had to hunt a bunch on google to find out how to fix this, although the fix is very easy.

For each value that you see in your file, just do a string substitution, like so:

:%s/<93>/\’/g

of course, you can’t just type that <93> in there, so to get it in there you use (via: http://www.vim.org/htmldoc/usr_45.html)

CTRL-V x 93

which inserts hex 93 in place.

In recently exported CSV’s from excel, I’ve seen hex 91-97.

Quite annoying, frankly.

You could also use perl, like here

perl -pi -e”s/\x92/’/g” myfile.html

and use it on multiple files.

Blogged with Flock

Tags: , , , , , , , , , , , , , ,

8 Comments so far

  1. Marc St-Jacques on June 3rd, 2009

    Thank you. You’re probably the only site in cyberspace that addresses this directly ;-) Perusing through the vim help files can be hell sometimes.

  2. Patrick Berkeley on July 28th, 2009

    Thanks for this. Although I don’t use vim it’s nice to see your rendition in perl.

  3. p1 on March 29th, 2010

    Thanks a lot!! found this after a lot of googling!

  4. thanks on August 9th, 2010

    exactly what i needed

    thanks!

  5. Jacqueline on October 19th, 2010

    Oh, you’re my hero! Thanks for writing this up!

  6. Thomas on April 22nd, 2011

    Thanks a lot for writing a short notice on this finding and for explaining the background (btw: the smart quotes are not MS specific – in my case they came from OpenOffice!).

    Luckily, your blog gets indexed by google :-)

  7. Kaolin Fire on December 21st, 2011

    And to find out what character something is if it’s not showing you (i.e. if your vim is actually showing you the curly-quotes instead of the hex values), you can type g8

    Or :set statusline=%b\ 0x%B
    And possibly :set laststatus=2 to make that visible

    ((from http://vim.wikia.com/wiki/Showing_the_ASCII_value_of_the_current_character ))

    And if you need to set the full unicode value instead of the two-byte hex value, it’s ctrl-v u—so ctrl-v u 201c, for instance.

  8. Aditya Nakhare on January 17th, 2012

    Thanks a lot.

Leave a Reply