Does anyone know of a vim plugin or script to convert special characters to their corresponding HTML entities?
I develop websites for corporate clients, so we see the ®, ™, etc. chars a whole lot. Sometimes I paste in huge blocks of copy, which might even contain pretty quotes (“ ”) or other strange characters from word processors.
So, my question is this: Does anyone know of a vim plugin or script that can, in one fell swoop, convert all these characters to html entities?
I think this covers all the bases of the entities it would be nice to have: http://web.forret.com/tools/charmap.asp
So, for the characters above, they would be replaced with ®
, ™
, “
, ”
, etc.
I tried the htmlspecialchars vimball (http://www.vim.org/scripts/script.php?script_id=2377), but no dice. It only performs its replacement like the PHP htmlsepcial开发者_如何学Cchars function, replacing html-conflicting characters, and doesn't cover any additional special characters.
I would recommend Tim Pope's unimpaired plugin. It provides commands to encode and decode html entities, using the mappings: [x
and ]x
respectively.
Perl is better for this sort of things. Paste your file into vim and run this:
:%!perl -p -i -e 'BEGIN { use Encode; } $_=Encode::decode_utf8($_) unless Encode::is_utf8($_); $_=Encode::encode("ascii", $_, Encode::FB_HTMLCREF);'
Or even better:
%!perl -p -i -e 'BEGIN { use HTML::Entities; use Encode; } $_=Encode::decode_utf8($_) unless Encode::is_utf8($_); $_=Encode::encode("ascii", $_, sub{HTML::Entities::encode_entities(chr shift)});'
(HTML::Entities is a part of HTML::Parser on my system)
you can do this as a macro, something like this in your .vimrc (i'll let you fill in the rest of the entities ;-) ):
let @a=':%s/á/\á/g^M:%s/é/\é/g^M:%s/í/\í/g^M ... '
note that ^M is a special character entered using Ctrl+V, Ctrl+M.
I was as surprised as you are that Vim can't do this and no plugins (not even unimpaired.vim) can convert all HTML entities.
So I made a plugin!
Usage: in normal mode, ]he<motion>
to convert the character included in the motion to their proper HTML entities. Will convert (almost any) character.
精彩评论