1

Is writing <meta charset=utf-8 /> in an HTML file (being edited by Emacs) sufficient, or do I also need to specify -*- coding: utf-8 -*- on the first line?

In other words, do I have to write

<!-- -*- coding: utf-8 -*- -->
<!DOCTYPE html>
<head>
    <meta charset=utf-8 />
</head>
<body>
    <p>你好,世界!</p>
    <p>Xin chào thế giới</p>
</body>

or is it sufficient to write

<!DOCTYPE html>
<head>
    <meta charset=utf-8 />
</head>
<body>
    <p>你好,世界!</p>
    <p>Xin chào thế giới</p>
</body>

to signal to Emacs that I'd like the file to be saved in utf-8?

I'm using web-mode.el for HTML pages (because it is the nicest way at this time to edit an HTML page that may also contain CSS and JS code).

And if you know the answer, you won't mind my trying to sneak a second question here: Why is Emacs (25.3.1) unable to properly encode display the Vietnamese text above (which SO handles just fine), even though the same Emacs handles the Chinese text with no difficulty?

  • Note that emacs -Q, version 26.1 (build 2, x86_64-pc-linux-gnu) displays the chinese characters in the html document just fine on my box. – Tobias Jun 21 at 16:23
  • The puzzle is that I can display Chinese characters just fine, and I can save/load perfectly fine also (provided I set utf-8 correctly for a text file; or now for an HTML file), but I cannot see some Vietnamese (the more exotic accented Latin characters) on Emacs, even after utf-8 has been set correctly—and confirmed to be set correctly because the Chinese characters are there. – Vrokipal Jun 21 at 17:19
1

The function sgml-html-meta-auto-coding-function selects the coding system on the basis of the meta tag.

It only checks the meta tag if the document begins with <!doctype html> or with <html>. (Case is not relevant since case-fold-search is let-bound to t.)

That function is a member of auto-coding-functions by default. So the coding system is automagically selected on the base of the meta tag by default when the file is read in.

  • Just to complete your answer (to those who may not be using web-mode): auto-coding-functions and sgml-html-meta-auto-coding-function are not conditional on being in web-mode; are they? – Vrokipal Jun 21 at 17:16
  • @Vrokipal I deliberatly left out any connection to web-mode. auto-coding-functions is independent of the chosen major-mode. – Tobias Jun 21 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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