how to change encoding of a buffer in emacs

Emacs is pretty good at determining the default encoding of the file or buffer that it displays. The encoding of the file is determined when the file is loaded and the same encoding is used to save the content back to the file.

read file in a specific encoding

You can specify the encoding to be used while opening a file. In order to do so, you will use the command universal-coding-system-argument which will let you select a coding system for the next command. You can follow it up with the command to open the file.

The command is mapped to keychord C-x C-m c and followed by the command. So to open a file in a specified encoding, you will use C-x C-m c C-x C-f…selecting the coding system along the way.

change encoding of the buffer

If the buffer is already open, then you can use the revert-buffer-with-coding-system command to reload the buffer with a different encoding.

M-x revert-buffer-with-coding-system

change encoding when saving the buffer

You can choose to save the buffer with a different encoding than the one that was determined at the time of loading. In order to do so, use the command set-buffer-file-encoding-system. This will change the encoding of the buffer which will be used to save the buffer.

If you want to save the file as well, then use the command that we used to open the file. So, the keychord to save the file will be

C-x C-m c RET C-x C-w RET

It is quite intuitive once you figure out what is happening.

list all encodings

If you want a list of all the encoding that are available in the system, then use the command list-coding-systems to do so.

M-x list-coding-systems