macos OS X 终端 UTF-8 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4606570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:33:21  来源:igfitidea点击:

OS X Terminal UTF-8 issues

macosvimutf-8sshterminal

提问by Sami

Okay, so I finally got myself a MacBook Air after 15 years of linux. And before I got it my big concern was UTF-8 support because no matter if I get files sent to me from windows or mac-clients theres always issues with encoding, while on ubuntu I can be sure that all output no matter what program will produce perfect utf-8 encoded data.

好的,所以我终于在 15 年的 linux 之后给自己买了一台 MacBook Air。在我得到它之前,我最关心的是 UTF-8 支持,因为无论我从 windows 还是 mac 客户端发送给我的文件总是存在编码问题,而在 ubuntu 上,我可以确保所有输出无论什么程序都会产生完美的 utf-8 编码数据。

And now on my second day (today) with OS X Im tearing my hair of by frustration. Why?

现在在我使用 OS X 的第二天(今天),我沮丧地撕裂了我的头发。为什么?

When I open Nano and type some swedish characters like ??? in it, it puts out blank characters at the end of the line (which i guess is the other byte in each character)

当我打开 Nano 并输入一些瑞典字符时,例如 ??? 在其中,它在行尾放出空白字符(我猜这是每个字符中的另一个字节)

When I open python and try using swedish characters, it does not output anything at all

当我打开 python 并尝试使用瑞典字符时,它根本不输出任何内容

When I connect to a Ubuntu server trough SSH I cant type ??? in bash, tough it works in VIM (still trough SSH). And in nano backspace does not work, but if check the box "Delete sends ctrl+H" in the Terminal preferences, backspace starts working in nano but stops working in VIM.

当我通过 SSH 连接到 Ubuntu 服务器时,我无法输入???在 bash 中,它在 VIM 中有效(仍然通过 SSH)。并且在 nano 退格键不起作用,但是如果在终端首选项中选中“删除发送 ctrl+H”框,退格键开始在 nano 中工作,但在 VIM 中停止工作。

I've tried unchecking all other encodings then UTF-8 in terminal preferences but that does not seem to work either.

我尝试在终端首选项中取消选中所有其他编码,然后取消选中 UTF-8,但这似乎也不起作用。

I'm sure that every non US-person must have the same issues, so hove do I fix them? I just want full UTF-8 support... :'(

我敢肯定,每个非美国人都必须有同样的问题,所以我要解决这些问题吗?我只想要完整的 UTF-8 支持... :'(

回答by angeldust

For me, this helped: I checked locale on my local shell in terminal

对我来说,这有帮助:我在终端的本地 shell 上检查了语言环境

$ locale
LANG="cs_CZ.UTF-8"
LC_COLLATE="cs_CZ.UTF-8"

Then connected to any remote host I am using via ssh and edited file /etc/profile as root - at the end I added line:

然后通过 ssh 连接到我正在使用的任何远程主机,并以 root 身份编辑文件 /etc/profile - 最后我添加了一行:

export LANG=cs_CZ.UTF-8

After next connection it works fine in bash, ls and nano.

下一次连接后,它在 bash、ls 和 nano 中工作正常。

回答by ismail

Go to Terminal -> Preferences -> Advanced (Tab)go down to Internationaland select Unicode (UTF-8)as Character Encoding.

转到Terminal -> Preferences -> Advanced (Tab)向下International并选择Unicode (UTF-8)Character Encoding

And tick Set locale environment variables on startup.

并打勾Set locale environment variables on startup

回答by danmichaelo

Unfortunately, the Preferences dialog is not always very helpful, but by tweaking around you should be able to get everything working.

不幸的是,首选项对话框并不总是很有帮助,但通过调整你应该能够让一切正常。

To be able to type Swedish characters in Terminal, add the following lines to your ~/.inputrc (most likely you must create this file):

为了能够在终端中输入瑞典语字符,请将以下几行添加到您的 ~/.inputrc (很可能您必须创建此文件):

set input-meta on
set output-meta on
set convert-meta off

This should do the work both with utf8 and other codings in bash, nanoand many other programs. Some programs, like tmux, also depends on the locale. Then, adding for instance export LC_ALL=en_US.UTF-8to your ~/.profilefile should help, but keep in mind that a few (mainly obscure) programs require a standard locale, so if you have trouble running or compiling a program, try going back to LC_ALL=C.

这应该做的工作都与UTF8和其它值编码bashnano和许多其他程序。某些程序,例如tmux,也依赖于locale. 然后,将例如添加export LC_ALL=en_US.UTF-8到您的~/.profile文件应该会有所帮助,但请记住,一些(主要是晦涩的)程序需要标准语言环境,因此如果您在运行或编译程序时遇到问题,请尝试返回LC_ALL=C.

Some references that may be helpful:

一些可能有用的参考资料:

回答by Urban Vagabond

The following is a summary of what you need to do under OS X Mavericks (10.9). This is all summarized in

以下是您在 OS X Mavericks (10.9) 下需要做的总结。这一切都总结在

http://hints.macworld.com/article.php?story=20060825071728278

http://hints.macworld.com/article.php?story=20060825071728278

  1. Go to Terminal->Preferences->Settings->Advanced.

    Under International, make sure the character encoding is set to Unicode (UTF-8).

    Also, and this is key: under Emulation, make sure that Escape non-ASCII input with Control-Vis unchecked (i.e. is notset).

    These two settings fix things for Terminal.

  2. Make sure your locale is set to something that ends in .UTF-8. Type localeand look at the LC_CTYPEline. If it doesn't say something like en_US.UTF-8(the stuff before the dot might change if you are using a non-US-English locale), then in your Bash .profileor .bashrcin your home directory, add a line like this:

    export LC_CTYPE=en_US.UTF-8
    

    This will fix things for command-line programs in general.

  3. Add the following lines to .inputrcin your home directory (create it if necessary):

    set meta-flag on
    set input-meta on
    set output-meta on
    set convert-meta off
    

    This makes Bash be eight-bit clean, so it will pass UTF-8 characters in and out without messing with them.

  1. 转到终端->首选项->设置->高级。

    International 下,确保字符编码设置为Unicode (UTF-8)

    此外,这是关键:在Emulation下,确保使用 Control-V 转义非 ASCII 输入未选中(即设置)。

    这两个设置修复了终端的问题。

  2. 确保您的语言环境设置为以.UTF-8. 键入locale并查看该LC_CTYPE行。如果它没有说类似的话en_US.UTF-8(如果您使用的是非美国英语语言环境,点之前的内容可能会改变),那么在您的 Bash.profile.bashrc您的主目录中,添加如下一行:

    export LC_CTYPE=en_US.UTF-8
    

    这将解决一般命令行程序的问题。

  3. .inputrc将以下行添加到您的主目录中(如有必要,请创建它):

    set meta-flag on
    set input-meta on
    set output-meta on
    set convert-meta off
    

    这使得 Bash 是 8 位干净的,因此它将传入和传出 UTF-8 字符而不会弄乱它们。

Keep in mind you will have to restart Bash (e.g. close and reopen the Terminal window) to get it to pay attention to all the settings you make in 2 and 3 above.

请记住,您必须重新启动 Bash(例如关闭并重新打开终端窗口)才能让它注意您在上面 2 和 3 中所做的所有设置。

回答by Arunas Bartisius

Short versatile answer (fits to other national languages, even Lithuanian or Russian)

简短的通用答案(适用于其他国家语言,甚至立陶宛语或俄语)

  • open Terminal
  • edit .profile in home direkctory - nano .profile
  • add line export LC_ALL=en_US.UTF-8
  • press Ctrl+x and Y (exit and save)
  • 打开终端
  • 在家庭目录中编辑 .profile - nano .profile
  • 添加行 export LC_ALL=en_US.UTF-8
  • 按 Ctrl+x 和 Y(退出并保存)

This solved for me even small country rare national characters. You may need to close and open Terminal to make changes effective.

这为我解决了即使是小国罕见的民族文字。您可能需要关闭并打开终端才能使更改生效。

Also if you like Linux behavior (use lot of Alt shortcuts like Alt+. or Alt+, in mc) then you should disable Mac style Option key function: Terminal->Preferences->Profiles->Keyboard and check box: Use Option as Meta key

此外,如果您喜欢 Linux 行为(在 mc 中使用很多 Alt 快捷键,例如 Alt+. 或 Alt+,在 mc 中),那么您应该禁用 Mac 风格的 Option 键功能:终端->首选项->配置文件->键盘和复选框: Use Option as Meta key

回答by azzid

My terminal was just acting silly, not printing out ???. I found (and set) this setting: enter image description here

我的终端只是装傻,没有打印出来???。我发现(并设置)了这个设置:在此处输入图片说明

Under Terminal -> Preferences... -> Profiles -> Advanced.

Terminal -> Preferences... -> Profiles -> Advanced

Seems to have fixed my problem.

似乎解决了我的问题。

回答by virtuz

To make nanowork as you want it to, try:

nano按照您的意愿工作,请尝试:

export LANG="UTF-8"

Or get a newer version of nanovia MacPorts:

或获取更新版本的nanovia MacPorts

# cf. http://www.macports.org/install.php
port info nano
port variants nano
sudo port install nano +utf8 +color +no_wrap

With respect to ssh & UTF-8 issues comment out SendEnv LANG LC_*in /etc/ssh_config.

关于 ssh 和 UTF-8 问题,请SendEnv LANG LC_*/etc/ssh_config.

See: Terminal in OS X Lion: can't write ??? on remote machine

请参阅:OS X Lion 中的终端:无法写入???在远程机器上

回答by ak2

Check whether nano was actually built with UTF-8 support, using nano --version. Here it is on Cygwin:

检查 nano 是否实际上是使用 UTF-8 支持构建的,使用nano --version. 这是在 Cygwin 上的:

nano --version
 GNU nano version 2.2.5 (compiled 21:04:20, Nov  3 2010)
 (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
 2008, 2009 Free Software Foundation, Inc.
 Email: [email protected]    Web: http://www.nano-editor.org/
 Compiled options: --enable-color --enable-extra --enable-multibuffer
 --enable-nanorc --enable-utf8

Note the last bit.

注意最后一点。

回答by chen bin

Since nano is a terminal application. I guess it's more a terminal problem than a nano problem.

由于 nano 是一个终端应用程序。我想这更像是一个终端问题而不是纳米问题。

I met similar problems at OS X (I cannot input and view the Chinese characters at terminal).

我在 OS X 上遇到过类似的问题(我在终端无法输入和查看汉字)。

I tried tweaking the system setting through OS X UI whose real effect is change the environment variable LANG.

我尝试通过 OS X UI 调整系统设置,其实际效果是更改环境变量 LANG。

So finally I just add some stuff into the ~/.bashrc to fix the problem.

所以最后我只是在 ~/.bashrc 中添加了一些东西来解决这个问题。

# I'm Chinese and I prefer English manual
export LC_COLLATE="zh_CN.UTF-8"
export LC_CTYPE="zh_CN.UTF-8"
export LC_MESSAGES="en_US.UTF-8"
export LC_MONETARY="zh_CN.UTF-8"
export LC_NUMERIC="zh_CN.UTF-8"
export LC_TIME="zh_CN.UTF-8"

BTW, don't set LC_ALL which will override all the other LC_* settings.

顺便说一句,不要设置 LC_ALL 这将覆盖所有其他 LC_* 设置。

回答by Dmitriy

Try

尝试

  1. Having a Powerline compatible font installed https://github.com/powerline/fonts
  2. Setting these ENV vars in .zshrcor .bashrc:
  1. 安装了 Powerline 兼容字体https://github.com/powerline/fonts
  2. .zshrcor 中设置这些 ENV 变量.bashrc
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"