Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use Emacs to edit an XML file that is also read by another application. The other application requires that the file start with a byte-order marker (BOM). However, Emacs seems to remove the BOM every time I edit the file. Is there a way to make Emacs leave the BOM alone?

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

Emacs will write a BOM or not depending on what coding system it is using. Emacs automatically chooses the coding system it uses when visiting a file.

You can change the coding system to utf-8-with-signature, which will tell Emacs to write the BOM.

To change the coding system of a visited file, `C-x RET r utf-8-with-signature RET'.

You can set the coding system that Emacs uses for a particular file by setting a file variable. See the fine manual section 57.3.4 Local Variables in Files to learn how to do that.

share|improve this answer
    
Awesome, exactly the kind of answer I was hoping for! Thank you! –  Vebjorn Ljosa Sep 17 '09 at 13:52
add comment

Followup on Richard Hoskins’s answer: if you never want the BOM to be hidden by emacs, you can disable the *-with-signature codings with this snippet:

(setq auto-coding-regexp-alist
  (delete (rassoc 'utf-16be-with-signature auto-coding-regexp-alist)
  (delete (rassoc 'utf-16le-with-signature auto-coding-regexp-alist)
  (delete (rassoc 'utf-8-with-signature auto-coding-regexp-alist)
          auto-coding-regexp-alist))))

The BOM is U+FEFF, the “zero-width non-breaking space”, and doesn’t show up as a box in my emacs 23.1.1—instead, the top line of the file’s moved slightly down, and a box sometimes appears around the first line—but you can see that the BOM’s there, and delete it if necessary.

share|improve this answer
add comment

Emacs "itself" should not mess with the BOM; if it's really doing that, then it would have to be the code implementing the Emacs "mode" you are useing to edit your XML files which removes the BOM. Since you don't say which one that is, I can only refer you to the documentation for that mode, or that you open the files in fundamental-mode (or similar non-destructive mode). Or try M-x find-file-literally if all else fails.

share|improve this answer
    
use xml-mode (i.e., sgml-mode), but that problem seems to be elsewhere: if I open the file with find-file-literally and then do M-x sgml-mode, the BOM is not removed. Because special characters are not UTF-8-encoded when visiting a file literally, it would be nice to figure out where in the underlying format conversion and character code conversion code the BOM is removed. –  Vebjorn Ljosa Sep 15 '09 at 12:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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