39

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?

ayckoster
  • 6,707
  • 6
  • 32
  • 45

3 Answers3

75

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?

Enh-ruby-mode comment encoding line

anquegi
  • 11,125
  • 4
  • 51
  • 67
crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • 9
    Yay - I provided the patch to `ruby-mode.el` which added that variable. :-) Nice to see I wasn't the only one annoyed by those comments. – sanityinc Jun 23 '11 at 17:31
0

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.

Joe
  • 2,352
  • 20
  • 38
  • 3
    UTF-8 is becoming the standard encoding everywhere, which is a Good Thing. You can configure Emacs to use UTF-8 everywhere by adding the following to your `~/.emacs` file: `(set-language-environment 'utf-8)`, `(set-default-coding-systems 'utf-8)`, `(set-selection-coding-system 'utf-8)`, `(set-locale-environment "en_GB.UTF-8")`, and `(prefer-coding-system 'utf-8)`. Furthermore, UTF-8 is the default encoding for Ruby 2, so if you have upgraded to it, and have set your Emacs environment to use UTF-8, there's no need for Emacs to automatically add the encoding line on save in Ruby mode. – Teemu Leisti Jul 16 '13 at 10:25
0

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.

mavit
  • 619
  • 6
  • 13