In some files, especially .rb I have a problem where emacs forces me to have the following encoding line.
# -*- coding: utf-8 -*-
Even if I delete the line it will get written again once I save my file.
Is it possible to disable this feature?
It looks like this is part of the ruby-mode in emacs.
I found a link to an article that shows how to edit the ruby-mode.el file. Not sure if it works, but there is also a comment on that article that may work better:
(setq ruby-insert-encoding-magic-comment nil)
If instead of using ruby-mode your are using enh-ruby-mode you should set this variable:
(setq enh-ruby-add-encoding-comment-on-save nil)
Links:
Fix: Emacs/Aquamacs keeps adding encoding comments to my files
Also, semi-related question but pertinent answer by Michael Kohl: How can I avoid putting the magic encoding comment on top of every UTF-8 file in Ruby 1.9?
If you've copied/pasted code from the Internet into an Emacs buffer, you might actually have non-ASCII characters. The # -*- coding: utf-8 -*-
is Emacs way of telling you that it can't simply encode the file as ASCII. Scan the code for potentially offending characters (in my case it was "smart quotes" copied from http://matteomelani.wordpress.com/2011/10/17/authentication-for-mobile-devices/. Of course Ruby barfed on them.
It is the default behaviour in Emacs 28 not to write this line if the encoding is UTF-8.
At the time of writing, Emacs 28 has not yet been released, but, when it is, perhaps in 2022, upgrading should solve the problem.