Change encoding linux terminal

How to get terminal’s Character Encoding

Now I change my gnome-terminal’s character encoding to «GBK» (default it is UTF-8), but how can I get the value(character encoding) in my Linux?

7 Answers 7

The terminal uses environment variables to determine which character set to use, therefore you can determine it by looking at those variables:

These environment variables are used by applications that are using the terminal for I/O. The terminal emulator itself has no knowledge of them whatsoever, and its currently effective character encoding is a setting somewhere within the emulator program (a data member inside a libvte class in the case of GNOME Terminal).

the ordering of variables suggested here is not good. a more complete solution would be something like: echo $>>. then again, the variable being set isn’t a guarantee that they’re valid, so you should stick to the locale program (as seen in other answers here).

As @JdeBP said, the terminal does not use the locale environment variables to determine its encoding. The terminal can however let applications that interact it know its encoding by setting the locale environment variables. For instance, on macOS you can choose the terminal encoding and optionally set the locale environment variables at terminal startup in Terminal > Preferences > Profiles > Advanced .

locale command with no arguments will print the values of all of the relevant environment variables except for LANGUAGE.

This is what worked for me on a CentOS system. It showed the system encoding based upon current language settings. The terminal settings used to get to that machine are a different story and a function of the client being used.

Check encoding and language:

$ echo $LC_CTYPE ISO-8859-1 $ echo $LANG pt_BR 
$ export LC_ALL=pt_PT.utf8 $ export LANG="$LC_ALL" 
python -c "import sys; print(sys.stdout.encoding)" 

Circumstantial indications from $LC_CTYPE , locale and such might seem alluring, but these are completely separated from the encoding the terminal application (actually an emulator) happens to be using when displaying characters on the screen.

They only way to detect encoding for sure is to output something only present in the encoding, e.g. ä , take a screenshot, analyze that image and check if the output character is correct.

So no, it’s not possible, sadly.

To see the current locale information use locale command. Below is an example on RHEL 7.8

[usr@host ~]$ locale LANG=en_US.UTF-8 LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL= 

Examination of https://invisible-island.net/xterm/ctlseqs/ctlseqs.html, the xterm control character documentation, shows that it follows the ISO 2022 standard for character set switching. In particular ESC % G selects UTF-8. So to force the terminal to use UTF-8, this command would need to be sent. I find no way of querying which character set is currently in use, but there are ways of discovering if the terminal supports national replacement character sets.

Читайте также:  Pki client для линукс

However, from charsets(7), it doesn’t look like GBK (or GB2312) is an encoding supported by ISO 2022 and xterm doesn’t support it natively. So your best bet might be to use iconv to convert to UTF-8.

Further reading shows that a (significant) subset of GBK is EUC, which is a ISO2022 code, so ISO2022 capable terminals may be able to display GBK natively after all, but I can’t find any mention of activating this programmatically, so the terminal’s user interface would be the only recourse.

Источник

Изменение кодировки терминала

Приветствую всех! Нужно изменить кодировку терминала и клавиатуры с UTF-8 на cp1251. Как это сделать? Какие файлы нужно править?

(если кто спросит, зачем это нужно: пишу программу, где надо работать с вводимыми с клавиатуры символами кириллицы, а символы кириллицы в UTF-8, в отличие от cp1251, нельзя закодировать в char)

В настройках терминала. В xfce4-terminal – ПКМ – Настройки – Дополнительно

Смотря какой у тебя эмулятор терминала.

А почему не используешь тогда wchar?

А в самом терминале нельзя поменять?

«Здравствуйте, помогите сломать ноги, чтобы ходить на костылях».

Конкретизирую вопрос. Сейчас при запуске /etc/init.d/keymaps restart выходит Setting keyboard mode [UTF-8] Что надо прописать в /etc/conf.d/keymaps, чтобы этот keyboard mode поменять?

Ты не путай системную локаль и локаль в твоем терминале. Если ты махнешь во всей системе на 1251, то может быть не очень потом. Поэтому лучше поменять в конкретном терминале, который ты используешь. Как пример тебе выше сказали, как сделать в терминале в крысе. Какой там у тебя — никто не знает, ты же не говоришь.

Пытаюсь, но получается какая-то лажа. Выводятся нормально, но wchar L»Б» и Б, введённый с клавиатуры через wcin — это почему-то оказывается разные символы

А как в системе махнуть? Какая переменная отвечает за keyboard mode. Локаль и LC_ALL в /etc/env.d/02locale менял, ничего не изменилось

Источник

Switch encoding of terminal with a command

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.

Читайте также:  Система доменных имен linux

Good question! Encoding problems are often a PITA. This is a way which won’t work. unix.stackexchange.com/questions/8859/…

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

Источник

How to change terminal character encoding

Today I uninstalled the GUI packages on my home CentOS installation. Now, when the machine starts, I get the login prompt. It acts finicky. Sometimes pressing enter just once after answering each prompt works. Other times, no. When I manage to get logged in, I can only enter commands in bash by entering one character at a time — that is:

l, enter o, enter c, enter a, enter l, enter e, enter enter 
speed 38400 baud; rows 64; columns 160; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts cdtrdsr -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke 

Did it act strangely before? (Don’t assume; if you don’t know, say so.) What reason do you have to suspect that this is related to character encoding?

I don’t know if the multi-user mode acted strange before. In the GUI, the ‘terminal window’ application always functioned normally. I thought maybe it was encoding (or even keymaps) because the keys were being interpreted differently.

I suspect that two programs are reading from the terminal at the same time, but I don’t know which ones that would be. When you have a chance, run ps on the console, or log in remotely and run ps -t tty1 .

ctrl+alt+f2 switched to tty2 and it does not have the issue. Now, how to fix what is up with tty1? Hmmm.

Читайте также:  Vnc linux разрешение экрана

1 Answer 1

Edit2: http://unixhelp.ed.ac.uk/CGI/man-cgi?stty can help with regard to the stty output. I’d particularly look at your «min = 1» and the different echo* settings

You may wish to try some invocatoin of stty (your example looks a bit like a «stty cbreak» or «raw» mode). Try entering : stty sane

once fixed (if it works) you’ll have an easier time to investigate

Edit1: After you added your stty -a settings, I’ll show you what differs between yours and mine.

(note that I do mine on an old AIX xterm, so of course there are many differences! But it could point out the relevant one. I let you do the work to see which could be the culpript, as I don’t have time right now, sorry!)

To do that, I listed all the keywords stty shows us in «WELIST», and then added a » » in front of every lines of each stty, and displayed them with :

for i in $( cat WELIST) ; do I_Have=$(grep "[ -]$i " IHAVE) You_Have=$(grep "[ -]$i " YOUHAVE) if [ "$I_Have" = "$You_Have" ] then printf "%-20s : %s\n" "$I_Have" "BOTH" else if [ -z "$I_Have" ] then printf "%-20s : %s\n" "$You_Have" "ONLY you" continue fi if [ -z "$You_Have" ] then printf "%-20s : %s\n" "$I_Have" "ONLY me" continue fi printf "%-20s : %s\n" "$I_Have" "me" printf "%-20s : %s\n" "$You_Have" "you" fi done 
 brkint : me -brkint : you bs0 : ONLY you cdtrdsr : ONLY you -clocal : BOTH 179 columns : me columns 160 : you cr0 : ONLY you cread : BOTH -crtscts : ONLY you cs8 : BOTH -cstopb : BOTH discard = ^O : ONLY me dsusp = ^Y : ONLY me echo : BOTH -echoctl : me echoctl : you -echoe : me echoe : you -echok : me echok : you -echoke : me echoke : you -echonl : BOTH -echoprt : BOTH eof = ^D : BOTH eol = : BOTH eol2 = : BOTH erase = ^? : BOTH eucw 1:1:0:0 : ONLY me ff0 : ONLY you flush = ^O : ONLY you -flusho : ONLY me -hupcl : me hupcl : you icanon : BOTH icrnl : BOTH -iexten : me iexten : you -ignbrk : BOTH -igncr : BOTH -ignpar : BOTH -imaxbel : BOTH -inlcr : BOTH -inpck : BOTH intr = ^C : BOTH isig : BOTH -istrip : BOTH -iuclc : BOTH iutf8 : ONLY you -ixany : BOTH -ixoff : BOTH ixon : BOTH kill = ^U : BOTH line = 0 : ONLY you lnext = ^V : BOTH min = 1 : ONLY you nl0 : ONLY you -noflsh : BOTH -ocrnl : BOTH -ofdel : BOTH -ofill : BOTH -olcuc : BOTH onlcr : BOTH -onlret : BOTH -onocr : BOTH opost : BOTH -parenb : BOTH -parext : ONLY me -parmrk : BOTH -parodd : BOTH -pending : ONLY me quit = ^\ : BOTH reprint = ^R : ONLY me 60 rows : me rows 64 : you rprnt = ^R : ONLY you scrw 1:1:0:0: : ONLY me speed 38400 baud : BOTH start = ^Q : BOTH stop = ^S : BOTH susp = ^Z : BOTH swtch = : ONLY you tab0 : ONLY you tab3 : ONLY me time = 0 : ONLY you -tostop : BOTH vt0 : ONLY you werase = ^W : BOTH -xcase : BOTH 

Источник

Оцените статью
Adblock
detector