2009-08-07

change file encoding with emacs, python,java

On Aug 7, 10:50 am, Christophe Jorssen wrote:
> Hello all,
>
> I'd like to know if it is possible to change the encoding of a buffer.
> For exemple, my buffer is encoded in ansinew and I'd like to recode it in
> utf-8.
>
> And, by the way, is it possible to "read a file with this encoding", that
> is automatically convert the encoding of a file when opened (say the file
> is encoded in ansinew and I'd like to have it in my buffer encoded in
> utf-8).
>
> I'm using
>
> GNU Emacs 23.1.50.1
>
> (emacs-snapshot), under unbuntu.
>
> Thanks a lot and sorry for my complete ignorance of the subject.

open the file, then call set-buffer-file-coding-system, then type the encoding system you want. Type Tab to see a list of possible values.

this page might help:

• Emacs and Unicode Tips
http://xahlee.org/emacs/emacs_n_unicode.html

you could also run a lisp program that changes all your files in a dir to utf-8 or any other encoding. you could do this in python or elisp.

For python, see:

• Converting a File's Encoding with Python
http://xahlee.org/perl-python/charset_encoding.html

for elisp, see:

• Emacs Lisp Examples
http://xahlee.org/emacs/elisp_examples.html

look for the section to-unix-eol there.
You'll need to change the line

(set-buffer-file-coding-system 'unix)
to
(set-buffer-file-coding-system 'utf-8)

on linux, there's a command line tool iconv that does this as well. Java solution is also there linked in the python page above.

Xah
∑ http://xahlee.org/

No comments:

Post a Comment