42

On my Mac terminal, printing UTF-8 works in general, but the less doesn't work correctly.

So this works correctly:

$  echo -e '\xe2\x82\xac'   
€

but piping it into less gives something like this:

$  echo -e '\xe2\x82\xac' | less  
<E2><82><AC>

How can this be fixed?

For diagnostics:

I'm using Mac OS 10.6.8. less version 418, Terminal 2.1.2 (273.1).

The output of my locale is this:

$ locale
LANG="en_US.UTF-8"
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL="C"

5 Answers 5

60

Okay, I found the answer after some googling. Apparently, LESSCHARSET needs to be set like this:

export LESSCHARSET=utf-8

Now less works fine for me.

4
  • 1
    I had the same problem on CentOS. This line also fixed it there. Aug 11, 2015 at 13:01
  • 15
    For me what solved the problem was using less -r (display "raw" control characters)
    – waldyrious
    Aug 15, 2016 at 13:39
  • 1
    This works in Debian 8 too, thanks! Nov 10, 2016 at 14:43
  • 3
    less -r does the same thing but also handles emojis correctly, which export LESSCHARSET=utf-8 does not. Jun 8, 2017 at 16:18
6

If you can see some unicode characters in less, but are unable to get less to display emoji, try upgrading less to a more recent version. On Mac OS X, I went from version 458 to 481 and that fixed my problem (for example, git log can now display emoji in commit messages).

If you have homebrew, you can replace the system less with a newer version by running brew install homebrew/dupes/less.

1
  • Thanks! With newer versions of homebrew, you just need to brew install less to get the upgrade. Jun 3, 2018 at 20:51
2

Works for me with

LANG=
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"
1
  • 3
    LC_CTYPE is the important one. However the rules less uses are bizarre: instead of retrieving the encoding from the locale, it looks for the string "utf-8" (or a few other possibilities) in its name.So you’ll need to use LESSCHARSET if you want some other encoding or if your locale name doesn’t match less’s preconceptions. Aug 7, 2012 at 22:25
2

I googled this and tried the following environment variables which worked for me:

export LC_ALL=en_US.UTF-8
export LANG=en_us.UTF-8

Seeing as I find the LC_ALL in multiple different answers I think this is the correct one. But maybe not the only right answer, there could of course be more correct answers to this question.

Anyway some more googling gave me this description for the variable:

LC_ALL This variable determines the values for all locale categories. The value of the LC_ALL environment variable has precedence over any of the other environment variables starting with LC_ (LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, LC_TIME) and the LANG environment variable.

source: http://pubs.opengroup.org/onlinepubs/007908799/xbd/envvar.html

Leading me to think this is the language variable to rule them all :)

2
  • Please feel free to post less' version number. Nov 5, 2014 at 16:19
  • sure, less 458 (GNU regular expressions) Nov 21, 2014 at 10:45
0

Just update you [less][1]

Do it with brew.

brew install homebrew/core/less

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .