5
  1. I created a text file by copying its different parts from different sources (webpages, other text files, pdf files) into gedit and saved it to the file. I wonder if that is the reason why I may have multiple encodings in the text file, see also here.

    How shall I avoid creating a text file with mixed encodings when copying its different parts from different sources into an editor such as emacs?

  2. Whenever I open the file in gedit, gedit can always show or decode every part of the text correctly.

    But when I open the file in emacs, there will be characters that can't be shown correctly. I wonder why emacs can't do that? Is it because gedit add some metadata to the text file about which encoding for which part, and these metadata can't be accessed by emacs?

  3. When I find the correct encoding for a part of the text file, and change the encoding of the text file by C-x RET R <found encoding>, and save the change to the file, it is expected that emacs still can't show the other parts of the text files with different encodings. Also gedit can't show or decode the entire file correctly. Is it because the other parts with other encodings were altered as well?

    How should I do to convert the text file with mixed encodings to a single encoding such as utf-8?

6

1) How shall I avoid creating a text file with mixed encodings when copying its different parts from different sources into an editor such as emacs?

Irrespective of the source, you can always set the encoding of a buffer by setting the variable buffer-file-coding-system. An easy way to do this is via the keybinding C-x RET f which prompts you for a file coding system.

2)But when I open the file in emacs, there will be characters that can't be shown correctly. I wonder why emacs can't do that?

Gedit probably uses UTF-8 by default or has a different order of checking encodings where the right one is checked first. In emacs, you can always prefer UTF8 by setting (prefer-coding-system 'utf-8). This puts UTF-8 on top of the list used for detecting encoding of a buffer.

3) Is it because the other parts with other encodings were altered as well?

Yes, C-x RET R runs revert-buffer-with-coding-system which sets the coding system of the whole buffer to be <found encoding> in your case. Emacs treats buffer encoding as a buffer-local-variable, which makes sense because a buffer/file can have only one encoding system. AFAIK the reason you cannot have two encoding systems is it is hard to detect where one ends and the other begins in a bitstream. You can use C-x RET f to set the file-coding-system to a universal one such as UTF-8.

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.