Switch encoding of terminal with a command

58,903

Solution 1

You are looking for luit which is designed for this purpose:

$ luit -encoding ISO-8859-1 ssh remote.host

It will convert the output of the ssh command to UTF-8 so you don’t need to change the encoding of the terminal.

Solution 2

On xterm, change the encoding by using control sequences. For instance, within bash you get the UTF-8 character set by running:

echo -n $'\e%G'

The ISO 8859-1 coding is accessible by running:

echo -n $'\e%@'

Solution 3

Gnome Terminal uses gconf to store settings. These can be manipulated with gconftool So, you can use:

gconftool --set --type=string /apps/gnome-terminal/profiles/Default/encoding ISO-8859-1

I've tested this on 12.04. For some reason, the menu ("Terminal"->"Set Character Encoding...") doesn't seem to get updated in real time, but the actual character encoding does correctly update.

Solution 4

First edit current locales in your favourite editor

sudo gedit /var/lib/locales/supported.d/local to something like

en_GB.UTF-8 UTF-8
en_US.UTF-8 UTF-8
en_GB.ISO-8859-1 ISO-8859-1
en_US.ISO-8859-1 ISO-8859-1

Then run sudo dpkg-reconfigure locales

Then set terminal with LANG=en_GB.ISO-8859-1

Hope that works

Solution 5

I don't know how to test it, but with Konsole on Kubuntu I can do the following:

1) Find application registered to dbus

$ qdbus

I use konsole so a simple qdbus | grep kons gives me org.kde.konsole

2) Find current sessions in Konsole

$ qdbus org.kde.konsole | grep Sessions
/Sessions
/Sessions/1

$ set | grep -i sessions
KONSOLE_DBUS_SESSION=/Sessions/1

3) Find useful methods

$ qdbus org.kde.konsole /Sessions/1 | grep -i codec
method QByteArray org.kde.konsole.Session.codec()
method bool org.kde.konsole.Session.setCodec(QByteArray codec)

4) Check current encoding

$ qdbus org.kde.konsole /Sessions/1 org.kde.konsole.Session.codec
UTF-8

5) Set encoding

$ qdbus org.kde.konsole /Sessions/1 org.kde.konsole.Session.setCodec ISO-8859-1
true

6) Check current encoding

$ qdbus org.kde.konsole /Sessions/1 org.kde.konsole.Session.codec
ISO-8859-1

Ubuntu will have a similar terminal program, but you'll have to do some puzzling yourself with correct servicename, path, method and arguments.

Share:
58,903

Related videos on Youtube

Tomas Aschan
Author by

Tomas Aschan

I am an engineering physicist from Stockholm, Sweden, with a passionate interest in programming and software architecture. Since creating my first program at age 12 (a VB6 app that showed a smiley when a button was clicked) I've spent many hours in front of my computer, watching screen casts and reading blogs about programming as well as trying all the new concepts out in my own programs. With a Master's degree in Engineering Physics from the Royal Institute of Technology in Stockholm, Sweden, I have deepened my modelling and reasoning skills, as well as had the opportunity to try out many different technologies and tools. I am currently working as a software engineer at Spotify, mostly massaging data to enable our internal research into developer productivity.

Updated on September 18, 2022

Comments

  • Tomas Aschan
    Tomas Aschan over 1 year

    One of the servers I quite often ssh to uses western encoding instead of utf-8 (and there's no way I can change that).

    I've started writing a bash script to connect to this server, so I won't have to type out the entire address every time, but I would like to improve this script so it also changes the encoding of the terminal window correctly.

    The change I need to do can be performed using the mouse by navigating to "Terminal"->"Set Character Encoding..."->"Western (ISO-8859-1)". Is there a terminal command that does the same thing, for the current terminal window/screen?

    To clarify:
    I'm not interested in ways of switching the locale of the system on the remote site - that system is administered by someone else, and I have no idea what stuff might depend on the latin-1 encoding there. What I want to do is to let this terminal window on my side switch character encoding to the above mentioned, in the same way I can do with my mouse and the menus.

    • Admin
      Admin about 12 years
      Good question! Encoding problems are often a PITA... This is a way which won't work... unix.stackexchange.com/questions/8859/…
    • Admin
      Admin about 11 years
      OFF how is it possible that my answer is accepted but the bounty went to another one? :) :(
    • Admin
      Admin almost 10 years
      @törzsmókus: You can read about the (new?) bounty rules here: blog.stackoverflow.com/2010/06/improvements-to-bounty-system If the bounty owner does not award the bounty within the 7 day bounty period, the same auto-award rules apply, as before: any answers to the question posted after the bounty started, with at least 2 upvotes, are eligible for auto-accept and earn half the bounty amount. If there aren’t any answers meeting that criteria, no bounty is awarded. — and, again, all bounties are completely independent of and unrelated to accepting an answer.
  • Tomas Aschan
    Tomas Aschan about 12 years
    Sorry - I meant to give you the bounty rep, but clicked the wrong post =(
  • Dragon warrior
    Dragon warrior about 12 years
    as the menu of gnome-terminal isn’t guaranteed not to change, I consider this a highly fragile hack, which might be the right thing when there is no actual solution. but there is one.
  • Dragon warrior
    Dragon warrior about 12 years
    but this changes the default encoding for all terminals, not just the one running the ssh connection script.
  • joseph_morris
    joseph_morris almost 10 years
    This is correct. However, I landed here looking for this bit from luit's man page: "Luit is usually invoked transparently by the terminal emulator." So, for those arriving here because you're getting wrong encoding coming through in your terminal, the normal way to fix it is to change the encoding through the terminal emulator.
  • Dragon warrior
    Dragon warrior almost 10 years
    @joseph_morris let me point you to the third paragraph of the question being answered. the OP did know how to change the encoding using the menu commands of the terminal emulator (that you call the 'normal' way) but he was looking for a CLI alternative.
  • joseph_morris
    joseph_morris almost 10 years
    Right, that's why I started out with "This is correct." However, this page is now one of the top hits for "terminal encoding", and I thought that bit of luit documentation would be helpful for others who arrive here trying to answer "Why is my terminal encoding messed up?" -- and that it is encoding usually changed in the emulator, not CLI.
  • Dragon warrior
    Dragon warrior almost 10 years
    ah, now I see your point.