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?
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.
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.
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.
In my test, editing a
UTF-8 file doesn't change the encoding and the BOM remains (
efbb bf). (nxml-mode)
Well, this may vary between
nxml-mode, or the version of emacs (24 vs 26). It says the mode on the bottom.
If you edit an Emacs an XML file encoded in unicode (
UTF-16 little endian), it will change the encoding to
UTF-16 big endian. Maybe that's what he's talking about.
But the BOM is still there, changed from
ffef, and the nulls are on the odd byte instead of the even byte. You can see it in hexl-mode.
Sample xml file. The encoding attribute directs the encoding when emacs saves it in xml-mode or nxml-mode. A future version will be patched to check the BOM first.
<?xml version="1.0" encoding="UTF-16"?> <hi />
It looks like Emacs takes
UTF-16BE, while Windows takes it as
UTF-16LE (BE and LE don't work in Emacs for the encoding attribute). The encoding attribute is probably key to the problems here.
Saving it in powershell will convert it back to utf-16le.
[xml]$xml = get-content test.xml; $xml.save('test.xml')
With encoding="UTF-16LE" and encoding="UTF-16BE", the bom is removed, making the file unrecognizable in emacs. This is a confirmed bug that will be patched: http://lists.gnu.org/archive/html/bug-gnu-emacs/2019-05/msg00892.html