I don't find the encoding of current file, how to display it?
You can see there are some Chinese characters in the file, but I don't know what the encoding it is.
Is there any way to let it always show on the emacs GUI?
You have several ways to get (and set) the buffer encoding:
You can see with the U in the mode-line that your buffer is in "Unicode", if you put the mouse over it, it will show in a tooltip the current buffer encoding.
you can also see the current encoding with C-hv buffer-file-coding-system RET
you can change the whole buffer encoding for next save with C-xRETf
you can also change the detected encoding to force an other one and reload the file with C-xRETr
you can set an encoding for the next I/O command only with C-xRETc
there are some other possibilities, take a look on C-xRETC-h
Fix and diagnose:
Inside a buffer, if you are interested by a single character encoding / details, put the point on a chinese char and C-uC-x= will help. (The same without the C-u shows only a few informations about the character, and the encoding is not part of it.)
examine the file by yourself:
you can open a text file without any decoding or heuristic with M-x find-file-literally
or you can go closer to the metal (hex editor) with M-x hexl-find-file
if the file is a mess with mixed encodings, you can fix portions with M-x recode-region
Related
It is my understanding that txt files do not have encoding information stored so text editors simply make educated guesses about encoding of a given text file and then display the file on screen using that guessed encoding. If the editor guessed right you get your text on the screen, if the editor guessed wrong, then you (sometimes) get gibberish. Am I getting this right so far?
Now on to my problem. I have my bank statements in a csv file. When I open it in MS Excel 14 (MS Office 2010), it recognises the encoding and displays the problematic work as "obračun". Great. When I open the file in Emacs 24.3.1, it fails to recognise the correct encoding and displays the problematic word as "obra鑾n". Not so great.
My question is: how do I tell Emacs which encoding the file is in?
Thanks.
From the Emacs Manual:
If Emacs recognizes the encoding of a file incorrectly, you can reread
the file using the correct coding system with C-x RET r
(revert-buffer-with-coding-system). This command prompts for the
coding system to use.
Give utf-16 a try.
I'll try to put this as clearly as possible, since I myself don't understand very well what's going on.
If I have a buffer opened in Emacs, and it's in, let's say UTF-8 (could be anything really), and I paste some text that is in another encoding (from a PDF for example), Emacs will CHANGE the original encoding (UTF8) to the new encoding...
This is a pain in the ass, because it screws up thousands of other text lines, just so the new line can be correctly displayed...
So I guess my question is, how can I tell Emacs to NEVER change the encoding of the file? If a character can't be represented in the present encoding, than just don't show it (or show it all messed up like usually happens).
Thanks
Specifying -*- coding: utf-8 -*- (or whatever encoding you want) at the top of the file will force that encoding for that particular file. The relevant manual page is here.
For a more systematic approach, you might want to investigate the docstring for the file-coding-system-alist variable, which forces encodings based on filenames.
I have an xml-file. When I open it with Emacs it displays chinese characters (see attachment). This happens on my Windows 7 PC with Emacs and Notepad and also on my Windows XP (see figure A). Figure B is the hexl-mode of A.
If I use the Windows XP PC of a collegue and open the file with Notepad there are no chinese characters but a strange character character. I saved it as txt-file and sent it by email to my Windows7-PC (see figure C). The strange character was replaced with "?". (Due to restriction I could not use the PC of my collegue and reproduce the notepad file with the strange character).
My questions: it seems that there are characters in the XML-file which creates problems. I don't know how to cope with that. Does anybody has an idea how I can manage this problem? Does it have something to do with encoding? Thanks for hints.
By figure B, it looks like this file is encoded with a mixture of big-endian and little-endian UTF-16. It starts with fe ff, which is the byte order mark for big-endian UTF-16, and the XML declaration (<?xml version=...) is also big-endian, but the part starting with <report is little-endian. You can tell because the letters appear on even positions in the first part of the hexl display, but on odd positions further down.
Also, there is a null character (encoded as two bytes, 00 00) right before <report. Null characters are not allowed in XML documents.
However, since some of the XML elements appear correctly in figure A, it seems that the confusion goes on through the file. The file is corrupt, and this probably needs to be resolved manually.
If there are no non-ASCII characters in the file, I would try to open the file in Emacs as binary (M-x revert-buffer-with-coding-system and specify binary), remove all null bytes (M-% C-q C-# RET RET), save the file and hope for the best.
Another possible solution is to mark each region appearing with Chinese characters and recode it with M-x recode-region, giving "Text was really in" as utf-16-le and "But was interpreted as" as utf-16-be.
For some reason, Emacs takes "UTF-16" in an xml file encoding attribute as big endian, while Windows takes "UTF-16" as little endian (like when exporting from Task Scheduler). Emacs will unknowingly convert LE to BE automatically if you edit and save an xml file. You can mouse over the lower left "U" to see the current encoding. encoding="UTF-16LE" or encoding="UTF-16BE" will ruin the file after saving (no BOM). I believe the latest version has this fixed.
<?xml version="1.0" encoding="UTF-16"?>
<hi />
The solution of legoscia using the possibility of Emacs to change encoding within a file solved my problem. An other possibility is:
cut the part to convert
paste in a new file and save it
open it with an editor which can convert encodings
convert the file and save it
copy the converted string and add (paste) to the original file where you cut the part to convert
In my case it worked with Atom, but not with Notepad++.
PS: The reason why I used this way is that Emacs could not open anymore this kind of corrupted files. I don't know why but this is another issue.
Edit 1: Since copy, paste and merge is cumbersome I found the solution how to open currupted files with emacs: emacs -q xmlfile.xml. Using emacs like legoscia suggested is the best way to repair such files.
Assume that I receive a Spanish text written in MS word and saved as plain text (.txt). Unfortunately, all the Spanish accents show up like this:
Un \372ltimo an\341lisis
Can anybody tell me how I can avoid this, or at least how I can replace these characters? They are simply not found by the replace-regexp-functions, otherwise I could write a little elisp function that replaces every occurence of them by the associated Spanish accented character.
This looks like ISO 8859-1 (Latin-1) encoding.
Visit the file with that coding system instead. If Emacs does not automatically identify the coding system, you can revisit the file with an explicit coding system with revert-buffer-with-coding-system (C-x RET r).
For example, if you are looking at the garbled file you describe,
C-x RET r
latin-1 RET
yes RET
Then you can set the coding system you want for saving (C-x RET f) and specifying something like utf-8.
Is there a setting in UltraEdit that allows me to see the encoding of the file?
In UltraEdit, the encoding that is being used to -display- the file, is shown in the status bar at the right somewhere, together with the line-ending type in use, for example, "U8-UNIX". You can also manually set as what encoding the file has to be displayed. In version 10 this is under menu View -> Set Code Page. You can also -convert- the actual codepage of the file under menu File -> Conversions.
If the file does not have a BOM header, a couple of bytes at the start of the file indicating the encoding, the -actual- encoding of the file, can only be guessed. And even if the file has a BOM header, there can still be encoding issues.
All text editors do this, and some are better at it than others. I haven't done a comparision to see which is best at it. At the moment (2012), I know UltraEdit fails to detect UTF-8 and other variants in 1000 line (or longer) text files if the first UTF-8 character only appears later in the document. It also fails to show the encoding properly when you set it manually.
Notepad++ is also not great at detecting it, but when you know the encoding, you can set it manually.
Sublime Text is, as far as I know, best at detecting the encoding, also in large files.
I think there are also some very good command line tools out there, ported from GNU to Windows, to detect encoding. My bet would be that that's going to be the best option.