Other coding systems with emacs
Facts - Editor: emacs
Sunday, 28 June 2009 08:00

This page was made for emacs version 22.

Suppose you have just pasted characters from another language into your file. You have saved the file but you want to save it again to another coding system, for example to "utf-8". I encountered this when editing a javascript file and adding characters with accents, such as é. When javascript had to output these characters in the title tag of a new HTML page these characters were not correctly displayed in my browser. Saving the javascript file to UTF-8 fixed this display problem.

To save a file with UTF-8 encoding first load the file into an emacs buffer. Then invoke the command mm-set-buffer-file-coding-system: "Alt-x mm-set-buffer-file-coding-system ENTER utf-8". If emacs can not find this command then simply use the command set-buffer-file-coding-system instead.

Finally save the buffer.

It would be nice if emacs recognizes the coding system of the saved file next time you visit it. I have been experimenting putting the following text anywhere at the first line of my files: "-*- coding: utf-8 -*-" (without the quotes).

But I would like every file to be utf-8 so I put the following in my .emacs file instead:

(prefer-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(setq default-buffer-file-coding-system 'utf-8)

Now I can still enter characters with accents using "Control-x 8". So type "Control-x 8 'e" if you need to type é. These key sequences appear to be broken on Windows XP if you put the following in your .emacs init file as well:

(set-keyboard-coding-system 'utf-8)

Setting the coding system in emacs to utf-8 breaks cutting and pasting text from emacs to other applications. Add the following to your .emacs file in order to restore correct pasting text from emacs (tested on Windows XP):

(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
;; The next line is only needed for the MS-Windows clipboard
(set-clipboard-coding-system 'utf-16le-dos)
Last Updated on Sunday, 26 July 2009 13:14