Converting quotes with MacVim and Python 02.22.2009

I hate writing VimScript —I just feel uncomfortable with it. As such, when writing Vim commands and extensions, I tend to use either Ruby or Python. In this case, I needed to easily sanitize text copied from Microsoft Word that contained curly smart quotes and curly apostrophes. The following likely isn’t the fastest or even best way to do it, but it was a quicky, easy command to write:

python << EOF
def zap():
    import vim

    def strip_gremlins(s):
        s = s.replace(u"\u201c", "\"").replace(u"\u201d", "\"") #strip double curly quotes
        s = s.replace(u"\u2018", "'").replace(u"\u2019", "'").replace(u"\u02BC", "'") #strip single curly quotes
        return s

    r = vim.current.range
    for index, line in enumerate(r):
        r[index] = strip_gremlins(line.decode('utf-8')).encode('utf-8')
command! -range=% Zap python zap()

The last last lets me type :Zap instead of having to type :python zap().