Hi, i'm trying to replace all occurrences of characaters like é, è, ê
etc ... by their corresponding htmlentities. To do that, i use the
following command:
%s/é/\é/g
The problem with that command is that i have to do that for all
characters. I was wandering if there's a way to do it with only one
command.
I don't know of any inbuilt "character-to-HTML-entity" function
in vim. However, if you've got a file that looks like
é,é
where each line consists of the character in question, a comma,
and the HTML representation, you can do something like
:%s/\(.*\),\(.*\)/:%s!\1!\\\2!ge
on this auxiliary file. This yields N lines worth of these :s//
statements that you would want to execute. You can then
:%y
to yank all those lines, switch to the buffer on which you want
to make these changes, and then execute (in normal mode, not
command mode)
@"
which will execute the contents of the scratch register as if it
was a macro.
You can modify the above ":%s/.../...!ge" statement to accomodate
whatever format your big list is in. You can use ^I if it's
tab-delimited, or whatever. The basic gist is that you want your
resulting file to look like
:%s!é!\é!ge
on each line. Once you have a file that looks like this, it's
simply a matter of running it on your target document.
If you just wanted to replace a handful of "funky" (grave & acute
accented, tilded, umlauted, etc) characters to their
un-funk-ified versions, one might be able to exploit
"similar-character" classes, such as
:%s/[[=a=]]/a/g
and just execute that for each of your letters (depending on your
language, this would be 5 vowels, plus a small smattering of
consonants if needed).
-tim