How to edit UTF-8 files in vim on windows

I'm trying to edit UTF-8 text files using vim (7.3) on windows (prompt/powershell).

My _vimrc file is pretty simple:

set encoding=latin-1
set fileencoding=utf-8

The problem is, when editing a file that already exists, it (aparently) works. But If I use vim to create a new file, it doesn't write the same contents.

Let's say, I open a file using vim myfile.txt, that have this content:

discussão

If I :wq it everything is ok.

Now, if instead I'm creating a new file, after saving and reopening I get:

discussão

If I edit it to look like the first and save it, I can edit the file any number of times without problems.


Solution 1:

Use

set encoding=utf-8

and read :help 'encoding'.

In short, this option defines how Vim is going to treat text internally. It is usually set automatically by Vim to match your system's locale. If it can't get that information (or if your locale is latin1), Vim sets it to latin1 which, obviously, sucks hard.

Your best bet is to explicitly set it to utf-8.

On the other hand, set fileencoding=utf-8 is not needed and probably counterproductive because it is set by Vim for each buffer.