Removing byte-order marks (BOMs) using Emacs
Asked Answered
V

1

44

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?

Vellicate answered 4/10, 2010 at 21:28 Comment(0)
P
70

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.

Philbo answered 5/10, 2010 at 0:46 Comment(4)
Actually this doesn’t work for me. If I change the encoding by set-buffer-file-coding-system utf-8-with-signature to add the BOM and then save the file with C-x C-s, the file is saved without the signature.Jukebox
How are you checking this? find-file-literally shows that the BOM is there for me (cat and other command line tools hide it)Philbo
As always. I’ve tried it numerous times and it happened every time. So I posted a comment. Now when I want to reproduce it, it does not happen and setting the BOM works fine. Sorry for being dumb. :( I was checking using hexdump.Jukebox
Right-clicking on the most left character of the mode line ("status bar") of a buffer -- i. e. the one where you see "mouse-3: Set coding system" when you hover over it -- also invokes set-buffer-file-coding-system, right?Fagot

© 2022 - 2024 — McMap. All rights reserved.