28

Suddenly some of my text files, I'm editing with Emacs had changed and special characters were shown in a strange way, e. g.

\344 instead of ä
\374 instead of ü ... and so on.

In another file I now have

ü for ü
ä for ä ....

(I don't know which event was the cause for that, maybe I've been doing something wrong.)

I'd like to convert the document back to its original state (my standard encoding is UTF-8), so that it is displayed correctly with ä, ö, ü, ... but I do not know how to do that with emacs.

32

If you haven't changed the file, you can try M-x revert-buffer-with-coding-system. One of latin-1 or utf-8 should work, depending on the file.

You can also mark the entire file with C-x h and then try M-x recode-region. It will ask you for Text was really in and But was interpreted as. For the first file in your question, it looks like it should be latin-1 and utf-8, and for the second example it should probably be the other way around, utf-8 and latin-1.

After you got it right, you can choose which coding system to save the file with using M-x set-buffer-file-coding-system (or C-x C-m f for short).

  • Unfortunately, I need that command quite often, so I'd like to define a keyboard shortcut to invoke recode-region (orig: latin-1, interpreted as utf-8-dos) with one keystroke. How can I define a little program, which does that and which I can map to a key? – MostlyHarmless Jan 21 '14 at 14:19
  • 1
    You could record it as a macro, and then name, bind and save it as described here: gnu.org/software/emacs/manual/html_node/emacs/… – legoscia Jan 21 '14 at 14:59
1

The easiest way I've found is:

C-x RET r utf-8 RET

  • Welcome to superuser: While this may or may not answer the question, it would be a better answer if you could provide some explanation ('which event was the cause, etc') and why your solution works with detail and easy to follow instructions (the OP may or may not understand what needs to be done and future readers could also be unsure). If you feel your answer is correct please do these things and re-edit. Please take a couple of minutes and read:- superuser.com/help .Answering: superuser.com/help/how-to-answer, again welcome to superuser.Thankyou – mic84 Jan 18 '18 at 2:53
0

I tried the top answer above, but it didn't quite entirely fix my problem. So to add onto that...

If the problem persists when you re-open the file, it could also be due to the variable enable-multibyte-characters being nil.

A new buffer will usually have this as t (which is why you can paste utf-8 characters into the buffer hassle-free).

To fix this, you'll need to do something like: (set-buffer-multibyte 't)

The result should be instantaneous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.