Emacs File/Character Encoding/Decoding FAQ

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page is a tutorial on file and character encoding/decoding in emacs.

If you are not familiar with what's encoding/decoding, see: UNICODE Basics: What's Character Encoding, UTF-8, and All That?.

How to open a file with specific coding system?

Open it normally, then call revert-buffer-with-coding-system, then type a coding system. Press Tab ↹ to list possible ones.

How to set a encoding system for saving file?

Call set-buffer-file-coding-system, then type the encoding system you want. Press Tab ↹ to see a list of possible values.

After you set a encoding system, you can save the file and it'll be saved in the new encoding system.

In a buffer, how to find out what encoding system was used to decode current file?

Check the value of the variable buffer-file-coding-system. You do that by calling describe-variableF1 v】.

emacs describe-coding-system
emacs describe-coding-system

How to find out what's the current coding system used for opening and saving files?

Call describe-coding-system then press Enter ↵.

How to permanently choose a encoding system in emacs for opening and saving?

Choose the menu 〖Options ▸ Mule (Multilingual Environment) ▸ Set Language Environment〗. After that, be sure to pull the menu 〖Options ▸ Save Options〗 to save the setting in your emacs init file.

Or, just put this in your emacs init file:

;; UTF-8 as default encoding
(set-language-environment "UTF-8")

What encoding systems emacs supports?

You can find out by calling list-coding-systems.

Is there a way to declare a file with a particular character encoding?

Yes. In the first line of your file, put -*- coding: utf-8 -*-. That way, each time emacs open the file, emacs will presume that the file is encoded in utf-8. The line can start with a comment character(s) of your language, such as “#”, “//”. This magic line is also adopted by Python.

(info "(emacs) File Variables")

Where can one read more about Unicode in emacs?

(info "(emacs) International")

To learn more about Unicode, see Unicode for Programers: Essays Index.

Here's More Info About Unicode

Like it?
Buy Xah Emacs Tutorial
or share
blog comments powered by Disqus