Investigate characters

4.9
(34)

VimTricks reader Scott wrote in asking for help changing smart quotes in a file to regular quotes. Smart quotes or “curly quotes” are the special characters used by modern prose editors in place of regular double and single quotation marks. They are stylized differently for the beginning and end of the quote:

these quotes are smartand so are these but 'these' and "these" are not

Scott’s case was one that many of us encounter: needing to replace these quotes with the traditional versions in some code or text file. Clearly, this is a case for Vim’s s substitution command. Something like this would work:

:s/[“”]/"/g

But how would we insert these characters into the regex? What if we can’t just copy paste them? How can we investigate strange or unfamiliar characters that we come across in our Vim buffer? As usual, Vim has a way:

  • ga – Show details about the character under the cursor
  • :as – Command mode version of the same

If we position our cursor over top of one of the smart quotes and press ga, Vim will provide detailed information about the character under the cursor. Included is the hex code for the character, exactly what we’ll need to be able to enter it into our substitution command:

<“> 8220, Hex 201c, Oct 20034, Digr "6

We can see that the opening quote has a hex code of 201c. Repeating on the closing quote we get 201d and now we have what we need to create our regex. To do this, we’d enter :s/[ to start our replacement and then press <ctrl-v> to enter the hex code followed by u and the code, 201c. Then <ctrl-v> u201d to enter the closing quote. Lastly the rest of the substitution command: /"/g.

I’ve put this whole workflow together into a brief screencast below.

Have any tips that you’d think would be worthwhile for your fellow VimTricks readers? Need help finding that one perfect tip for your use case? Email us!

How useful was this tip?

Average rating 4.9 / 5. Vote count: 34

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

81 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

1 thought on “Investigate characters”

Leave a reply

Your email address will not be published. Required fields are marked *