Emacs File Encoding FAQ

By Xah Lee. Date: . Last updated: .

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

If you don't know what's encoding/decoding, see: Unicode Basics: Character Set, Encoding, UTF-8, Codepoint.

Set default encoding system for opening and saving

Put this in your Emacs Init File:

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

List emacs file encoding systems

Alt+x list-coding-systems

How to declare a file with a specific encoding

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")

How to open file with specific encoding

Open it normally, then Alt+x 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

Alt+x 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.

What is the encoding system used for current file

Check the value of the buffer local variable buffer-file-coding-system. You do that by Alt+x describe-variable.

What is the default coding system for opening and saving files

Alt+x describe-coding-system

emacs describe-coding-system 2021-08-27
emacs M-x describe-coding-system

See also:

Unicode and Font

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Tutorial

Quick Start

Manage Windows



Find Replace




Rectangle Edit

Line Wrap


View Special File

Editing Brackets

Org Mode


Emacs Efficiency