用 MacVim 打开包含中文的 lua 文件时出现乱码
如何设置MacVim显示代码。
当我打开在 Windows XP 中创建的 lua 文件时,这是一团糟。
g控制模式=0; -- 1£º¿ªÆôÖØÁ¸ÐÓ¸££ 0:¿ª´¥ÆÁ£Ê½
gState = GS_GAME;
总时间 = 0; --μ±Ç°1Ø¿¡»¡¡×Üʱ¼ä
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的文本看起来像是此文本的 CP936 编码(或 EUC-CN 或 GB18030 编码1)的 Latin-1(或 ISO-8859-1、CP819)解码2 :
打开文件时,Vim 会尝试
文件编码
选项。通常,latin1
是此列表中的最后一个值;读取 Latin-1 总是会成功,因为它是映射所有 256 个值的 8 位编码。因此,Vim 将您的 CP936 编码文件作为 Latin-1 打开。您可以通过多种选择让 Vim 使用其他编码:
您可以使用 :edit 命令的 "nofollow">
++enc=
选项(这将导致 Vim 忽略fileencodings
列表缓冲区):您可以通过省略路径将此应用到已加载的文件:
<前><代码>:e ++enc=cp936
您可以将您喜欢的编码添加到
latin1
之前的fileencodings
中(例如在您的~/.vimrc
中):您可以将
encoding
选项设置为您想要的编码。通常不鼓励这样做,因为它具有广泛的影响(请参阅:help编码
)。如果可能的话,将文件切换为 UTF-8 可能是有意义的,因为许多编辑器会正确自动检测 UTF-8。一旦你正确加载了文件(见上文),Vim 就可以像这样进行转换(设置
fileencoding
,然后:write
):Vim 应该几乎自动处理读取和处理编写 UTF-8 文件(
encoding
默认为 UTF-8,并且utf-8
在默认的fileencodings
中),但是如果您使用其他编辑器(即任何 Windows 编辑器编辑/创建了 CP936 文件),您可能需要将它们配置为使用 UTF-8 而不是(例如)CP936。1 我不熟悉中文文本使用的编码,这些编码似乎与“预期”文本相同。
2 我不懂中文,但全宽冒号和全宽逗号(以及 Google 对此文本的翻译)的存在和位置让我认为这是您所期望的文本。
The text you posted seems like a Latin-1 (or ISO-8859-1, CP819) decoding of the CP936 encoding (or EUC‑CN, or GB18030 encodings1) of this text2:
When opening a file, Vim tries the list of encodings specified in the
fileencodings
option. Usually,latin1
is the last value in this list; reading as Latin-1 will always be successful since it is an 8-bit encoding that maps all 256 values. Thus, Vim is opening your CP936 encoded file as Latin-1.You have several choices for getting Vim to use another encoding:
You can specify an encoding with the
++enc=
option to Vim’s:edit
command (this will cause Vim to ignore thefileencodings
list for the buffer):You can apply this to an already-loaded file by leaving off the path:
You can add your preferred encoding to
fileencodings
just beforelatin1
(e.g. in your~/.vimrc
):You can set the
encoding
option to your desired encoding. This is usually discouraged because it has wide-ranging impacts (see:help encoding
).It might make sense, if possible, to switch your files to UTF-8 since many editors will properly auto-detect UTF-8. Once you have the file loaded properly (see above), Vim can do the conversion like this (set
fileencoding
, then:write
):Vim should pretty much automatically handle reading and writing UTF-8 files (
encoding
defaults to UTF-8, andutf-8
is in the defaultfileencodings
), but if you are using other editors (i.e. whatever Windows editor edited/created the CP936 file(s)), you may need to configure them to use UTF-8 instead of (e.g.) CP936.1 I am not familiar with the encodings used for Chinese text, these encodings seem to be identical for the “expected” text.
2 I do not read Chinese, but the presence and locations of the FULLWIDTH COLON and FULLWIDTH COMMA (and Google's translation of this text) make me think this is the text you expected.