Removing byte-order marks (BOMs) using Emacs

rafl picture rafl · Oct 4, 2010 · Viewed 9.2k times · Source

I have a file containing UTF-8 encoded text with a byte-order mark. This BOM is getting in the way of things, and I'd like to remove it.

Resorting to either other tools, such as perl or awk, or weird editing modes, such as hexl-mode, every time I want to get rid of a BOM is somewhat annoying.

Is there a way to tell Emacs to remove the existing BOM and not to write it out to disk again on subsequent saves?

Answer

JSON picture JSON · Oct 5, 2010

Setting coding-system-for-write directly is a bit of a hack. The user command for changing a file's encoding (in this case from utf-8-with-signature to plain utf-8) is set-buffer-file-coding-system. It is bound by default to C-x RET f.