Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset emacs to save files in utf-8-unix character encoding?

I have a problem. I found out that emacs recently stopped to save all my new files with the default character set "utf-8-unix". I do not understand what I did, but when I open a file, above the mini-buffer I see "--:---" instead of "-U:---", where the "U" says that the file is saved with utf-8-unix charset. How can I reset emacs to save files in the proper coding system???

like image 995
tonix Avatar asked Dec 21 '13 20:12

tonix


People also ask

How do I change encoding in Emacs?

You can change the encoding to use for the file when saving using 'C-x C-m f' . You can also force this immediately by using 'C-x C-m c <encoding> RET C-x C-w RET' . You can list all available encodings with 'M-x list-coding-systems' . You can force Emacs to read a file in a specific encoding with 'C-x RET c C-x C-f' .

What is character encoding in Linux?

The most commonly used encodings are UTF-8, UTF-16 and the now-obsolete UCS-2. UTF-8 is a character encoding capable of encoding all possible characters, or code points,. Defined by Unicode and originally designed by Ken Thompson and Rob Pike. The encoding has a variable length and uses 8-bit code units.


2 Answers

Here is my setup:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ENCODING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C-h C RET
;; M-x describe-current-coding-system

(add-to-list 'file-coding-system-alist '("\\.tex" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("\\.txt" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("\\.el" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("\\.scratch" . utf-8-unix) )
(add-to-list 'file-coding-system-alist '("user_prefs" . utf-8-unix) )

(add-to-list 'process-coding-system-alist '("\\.txt" . utf-8-unix) )

(add-to-list 'network-coding-system-alist '("\\.txt" . utf-8-unix) )

(prefer-coding-system 'utf-8-unix)
(set-default-coding-systems 'utf-8-unix)
(set-terminal-coding-system 'utf-8-unix)
(set-keyboard-coding-system 'utf-8-unix)
(set-selection-coding-system 'utf-8-unix)
(setq-default buffer-file-coding-system 'utf-8-unix)

;; Treat clipboard input as UTF-8 string first; compound text next, etc.
(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))

;; mnemonic for utf-8 is "U", which is defined in the mule.el
(setq eol-mnemonic-dos ":CRLF")
(setq eol-mnemonic-mac ":CR")
(setq eol-mnemonic-undecided ":?")
(setq eol-mnemonic-unix ":LF")

(defalias 'read-buffer-file-coding-system 'lawlist-read-buffer-file-coding-system)
(defun lawlist-read-buffer-file-coding-system ()
  (let* ((bcss (find-coding-systems-region (point-min) (point-max)))
         (css-table
          (unless (equal bcss '(undecided))
            (append '("dos" "unix" "mac")
                    (delq nil (mapcar (lambda (cs)
                                        (if (memq (coding-system-base cs) bcss)
                                            (symbol-name cs)))
                                      coding-system-list)))))
         (combined-table
          (if css-table
              (completion-table-in-turn css-table coding-system-alist)
            coding-system-alist))
         (auto-cs
          (unless find-file-literally
            (save-excursion
              (save-restriction
                (widen)
                (goto-char (point-min))
                (funcall set-auto-coding-function
                         (or buffer-file-name "") (buffer-size))))))
         (preferred 'utf-8-unix)
         (default 'utf-8-unix)
         (completion-ignore-case t)
         (completion-pcm--delim-wild-regex ; Let "u8" complete to "utf-8".
          (concat completion-pcm--delim-wild-regex
                  "\\|\\([[:alpha:]]\\)[[:digit:]]"))
         (cs (completing-read
              (format "Coding system for saving file (default %s): " default)
              combined-table
              nil t nil 'coding-system-history
              (if default (symbol-name default)))))
    (unless (zerop (length cs)) (intern cs))))
like image 88
lawlist Avatar answered Nov 15 '22 05:11

lawlist


For some reason, Windows started interpreting my init.el file as being encoded in something other than UTF-8, and choked on characters such as "ö" and "§". The solution was to add a line ; -*- coding: utf-8 -*- at the start of the file.

To make very sure that UTF-8 is used in every case, I have the following lines in init.el:

;; Use UTF-8 for all character encoding.
(set-language-environment 'utf-8)
(set-default-coding-systems 'utf-8)
(set-selection-coding-system 'utf-8)
(set-locale-environment "en.UTF-8")
(prefer-coding-system 'utf-8)
(setq utf-translate-cjk-mode nil) ; disable CJK coding/encoding
like image 26
Teemu Leisti Avatar answered Nov 15 '22 06:11

Teemu Leisti