0

I have UTF-8 encoded HTML with id(s) and href(s) encoded as hex values like %D0%B0%D0%B1 or .D0.B0.D0.B1. Can I convert such strings into readable form using Vim?

3
  • Vim can't do that on its own out of the box, if that's what you are asking.
    – romainl
    Commented Mar 23, 2023 at 9:59
  • 1
    I'm not sure I grab what is the content of the html file. What is the code you have in your file? What if you go to one of this character and hit ga? How they are currently displayed in Vim (a screenshot would help)? How do you want to see them? Commented Mar 23, 2023 at 13:44
  • May be I was not clear but HTML is UTF-8; text is UTF-8 but id's and href's in tags are encoded as... you read.
    – hobo-mts
    Commented Mar 24, 2023 at 13:45

3 Answers 3

0

You can convert some special codes (e.g. %80 to %FF into 0x80 to 0xFF) with the following command:

:%s/\([\x80-\xFF]\)/\='0x'.printf('%02x', char2nr(submatch(1)))/g

The following question could be worth reading.

0

If the file contents are really %D0%B0%D0%B1 verbatim, then vim cannot do it directly. You'll first need to convert the 3-byte string %D0 into the byte \xd0. You can do it via vivian's command , butthe other way round like

:%s/%\([A-Za-z0-9]\{2}\)/\=nr2char(str2nr(submatch(1), 16))/g

Conduct the substitution, save the file (dont forget to leave a backup), and try to open the file again.

Then if you're seeing something like ç<99>½è<80><81>, it is because you're opening the file with the wrong encoding. For example opening a file containing Chinese characters with latin1 encoding results in junk like that.

To view the contents in human-readable form,

  1. Find out the actual encoding for the file, be it UTF8 or something else. It's not specified in the file, but you can guess. Guess with utilities like file -i or enca, and also you can guess based on where you got the file (like utf8 and cp936 are most likely for Chinese documents).
  2. Open the file vim some-utf8.html, and change the encoding to utf8 by :e ++enc=utf8. You can check the encoding used by vim to open the file with :set fenc.
0

I've Vim compiled with +perl, so I made

:perldo s/[.%]([0-9a-f]{2})/chr(hex($1))/ige

and that do what I want. But is's not clear for me.

1
  • It's magic: pairs of octets turns into one UTF-8 char. This subst converts only byte representation. But in Vim they become ONE char. Magic.
    – hobo-mts
    Commented Mar 25, 2023 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.