50

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.

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

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)

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

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.

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

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... :'(

Sami
  • 531
  • 1
  • 5
  • 3
  • 6
    For me, I had to uncheck _Escape non-ASCII input with Control-V_ under `Terminal->Preferences->Settings->Advanced` to allow me to directly type non-ASCII characters (e.g. `Option-U u` to get `ü`). – Urban Vagabond Jul 16 '14 at 00:02

12 Answers12

44

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

$ 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:

export LANG=cs_CZ.UTF-8

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

Simson
  • 3,373
  • 2
  • 24
  • 38
angeldust
  • 441
  • 4
  • 2
  • 17
    Many users will want to use the "English (US)" locale instead of Czech: "en_US.UTF-8". – Ivan Vučica Sep 11 '11 at 17:38
  • Yes, please update this answer to include the most common English local: en_US.UTF-8 which worked for me when I put it in LANG. – Chris Quenelle Nov 03 '17 at 18:07
  • /etc/profile contains a note: "It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates." – pavelety Apr 09 '21 at 12:21
21

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

And tick Set locale environment variables on startup.

ismail
  • 46,010
  • 9
  • 86
  • 95
  • Then I can confirm it works with vi/python here but _not_ nano. – ismail Jan 05 '11 at 16:43
  • There was this "encoding" tab on the top menu which is very misleading, setting the encoding in the menu you pointed fixed all my issues (even though environment variables were properly set in terminal to UTF-8) – Christophe Fondacci Jun 29 '14 at 22:01
17

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

  • open Terminal
  • edit .profile in home directory - nano .profile or in Catalina or newer nano .zshenv
  • add line export LC_ALL=en_US.UTF-8
  • press Ctrl+x and Y (exit and save)

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

Arunas Bartisius
  • 1,879
  • 22
  • 23
16

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

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-V is unchecked (i.e. is not set).

    These two settings fix things for Terminal.

  2. Make sure your locale is set to something that ends in .UTF-8. Type locale and look at the LC_CTYPE line. 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 .profile or .bashrc in 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 .inputrc in 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.

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.

Urban Vagabond
  • 7,282
  • 3
  • 28
  • 31
16

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):

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, nano and many other programs. Some programs, like tmux, also depends on the locale. Then, adding for instance export LC_ALL=en_US.UTF-8 to your ~/.profile file 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.

Some references that may be helpful:

danmichaelo
  • 1,706
  • 1
  • 25
  • 30
5

To make nano work as you want it to, try:

export LANG="UTF-8"

Or get a newer version of nano via 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.

See: Terminal in OS X Lion: can't write åäö on remote machine

Community
  • 1
  • 1
virtuz
  • 51
  • 1
  • 1
  • 1
    This works for me: (1) comment out `SendEnv LANG LC_*` in my local /etc/ssh_config AND (2) execute `export LANG=en_US.UTF-8` at remote host. – adbie May 09 '13 at 06:14
5

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

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

Seems to have fixed my problem.

azzid
  • 371
  • 2
  • 8
4

Check whether nano was actually built with UTF-8 support, using nano --version. Here it is on 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: nano@nano-editor.org    Web: http://www.nano-editor.org/
 Compiled options: --enable-color --enable-extra --enable-multibuffer
 --enable-nanorc --enable-utf8

Note the last bit.

ak2
  • 6,629
  • 31
  • 27
1

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

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

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

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

# 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.

chen bin
  • 568
  • 4
  • 16
1

Try

  1. Having a Powerline compatible font installed https://github.com/powerline/fonts
  2. Setting these ENV vars in .zshrc or .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"
Dmytro
  • 5,443
  • 2
  • 52
  • 50
1

Just add a file on remote server

$ sudo nano /etc/environment

LANG=en_US.utf-8
LC_ALL=en_US.utf-8

PS: Top answer has a suggestion to change /etc/profile file on remote server, it works, but this file is often overwritten by system, and doesn't help for long.

/etc/profile file contains disclaimer:

It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates.

pavelety
  • 746
  • 6
  • 8
0

In my case, simply using the uxterm command instead of xterm solved the problem. It's available in /opt/X11/bin/uxterm by installing the XQuartz package provided by Apple.

Jay
  • 61
  • 5