简体   繁体   中英

How can I make vim recognize the file's encoding?

I noticed that most of the time, when using some encoding other than 'the standard english', vim doesn't recognize and does not display characters correctly.
This is most easily seen by opening some ascii graphics, or similar files off the.net, which use cp437 code page.

Is there a way to make vim check for encoding when opening a file, and open it with a correct one?

What encodings do you use, as the most "portable" ones (the ones which the largest amount of users will have least problems with)?

Vim needs to detect the encoding, and that's going to be problematic, since files don't often explicitly state their encodings (an obvious exception are XML files with an encoding attribute in the header).

You can force Vim to reload a file with a different encoding thus:

:e ++enc=cp437

and you can set the default encoding in your.vimrc if you wish.

This page has more info and links, especially wrt. editing Unicode. UTF-8 is the most widely-used encoding, and the default you should probably go for.

You can use a vim modeline to set the file's encoding. This is simply a comment, in the first five lines of the file, that starts with vi: set fileencoding=cp437 .

You could also start with 'vim:', instead of 'vi: set', but the latter makes it compatible with more editors. You definitely need the space between either of these prefixes and 'fileencoding', or whatever option you want to set. The fileencoding option should solve your problem, though.

So, in Python or an.rc file, you can put this at the top of your file:

# vi: set fileencoding=cp437

In Java, C, C++, JavaScript, etc. put this:

// vi: set fileencoding=cp437

For more information, in vim, type :help modeline .

You can set the variable 'fileencodings' in your.vimrc.

This is a list of character encodings considered when starting to edit an existing file. When a file is read, Vim tries to use the first mentioned character encoding. If an error is detected, the next one in the list is tried. When an encoding is found that works, 'fileencoding' is set to it. If all fail, 'fileencoding' is set to an empty string, which means the value of 'encoding' is used.

See :help filencodings

If you often work with eg cp437 or cp1252, you can add it there:

set fileencodings=ucs-bom,utf-8,cp1252,default,latin9

You can encode your files using unicode, and set a Byte Order Mark (BOM) in the file. This will make vim treat it appropriately, but some compilers and programs may have trouble with it. Even basic shell commands like cat may misbehave for some use cases.

To do it, type this in vim:

:set fileencoding=utf-8
:set bomb
:w

For more information, type:

:help bomb

Add at the end of your.vimrc: (you can replace "utf-8" by "latin1" if needed)

if len(&fenc) == 0
  silent! exe "e! ++enc=utf-8" 
endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM