Special Characters in Vim
It took me two or three false starts to really get working in Vim. I loved Sublime Text so much that it was hard to switch. But when I would watch my coworker type at dizzyingly fast speeds, I knew that I would have to make the switch to Vim in order to maximize my efficiency at work.
It has mostly been a great ride, but once in a while, Vim gets weird or wonky. Today, I had a problem that a good deal of Google searching didn’t seem to want to fix, so I stumbled on a solution I thought I’d share in case anyone else is pulling their hair out.
I was working in Sublime (don’t ask) reworking some HTML markup on a particular page, and I had decided to convert it to HAML for inclusion in our app at work. I used the html2haml gem to convert it, and when I opened it up in Vim, I saw a number of special characters that looked like this: <200b>
. They were all over, littering the document. I didn’t want to try to remove them by hand, but trying to find and replace them with an empty string didn’t work, because Vim wouldn’t let me search for <200b>
.
I did some searching, and found out that this character is the zero-width space. This oxymoronically named unicode character is, I gather, the equivalent of the
HTML entity in that, to quote Wikipedia, “In HTML pages, this space can be used as a potential line-break in long words.”
Well, whatever it is, I had to switch Vim to utf-8 mode (:set encoding=utf8
). That enabled me to search for <200b>
. After that, it was simply a matter of finding and replacing: :%s/<200b>///g
. You’re probably familiar with that syntax, but let’s break it down. :%s/old_string/new_string/options
. That is, :
puts Vim into execute mode, %s/
tells it you’re going to search, <200b>
is the search string, and we’re replacing it with nothing, hence the next two slashes: //
, and the g
flag stands for global, in other words, do this throughout the file.
Note that, in order to include <200b>
in the string search, I had to physically highlight it with my mouse and copy paste it into the search; yanking or using visual mode did not work.
I hope that was able to save someone a headache.