Backspace deletes only part of a unicode character

I just discovered a really annoying issue with my terminal.

$ locale
LANG=hr_HR.UTF-8
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C

$ stty -a | sed -n -e 's/^.*\; erase \= //p' | cut -d\; -f1
^?

If you type any number of non-English Unicode characters, e.g. “öäüčđž” and then use your backspace key to delete those, then immediately start typing any of valid commands, the result will be quite unexpected:

$ ls -al
bash: $'\303\266\303\244\303\274\304ls': command not found

Only parts of each Unicode character are deleted due to the setting stty erase ^?.
I tried to set stty erase ^H, by using Ctrl+V+backspace, but that didn’t resolve my issue. Any ideas?

Change LC_CTYPE="C" to LC_CTYPE="hr_HR.UTF-8" and reboot:

sudo localectl set-locale LC_CTYPE="hr_HR.UTF-8"

More info:

https://unix.stackexchange.com/questions/180706/bash-ls-command-not-found-when-typing-ñ-by-mistake

Unfortunately changing LC_CTYPE with localectl didn’t fix it.

$ sudo localectl set-locale LC_CTYPE="hr_HR.UTF-8"^C
$ localectl 
   System Locale: LANG=en_US.UTF-8
                  LC_CTYPE=hr_HR.UTF-8
                  LC_NUMERIC=hr_HR.UTF-8
                  LC_TIME=hr_HR.UTF-8
                  LC_MONETARY=hr_HR.UTF-8
                  LC_PAPER=hr_HR.UTF-8
                  LC_NAME=hr_HR.UTF-8
                  LC_ADDRESS=hr_HR.UTF-8
                  LC_TELEPHONE=hr_HR.UTF-8
                  LC_MEASUREMENT=hr_HR.UTF-8
                  LC_IDENTIFICATION=hr_HR.UTF-8
       VC Keymap: us
      X11 Layout: us
       X11 Model: pc105
$ locale   
LANG=hr_HR.UTF-8
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C
$ locale -a
C
POSIX
en_US.utf8
hr_HR.utf8

Try adding LC_CTYPE=hr_HR.UTF-8 to /etc/locale.conf

I didn’t have to, since my locale.conf already contained it.

$ cat /etc/locale.conf  
LANG=en_US.UTF-8
LC_CTYPE=hr_HR.UTF-8
LC_NUMERIC=hr_HR.UTF-8
LC_TIME=hr_HR.UTF-8
LC_MONETARY=hr_HR.UTF-8
LC_PAPER=hr_HR.UTF-8
LC_NAME=hr_HR.UTF-8
LC_ADDRESS=hr_HR.UTF-8
LC_TELEPHONE=hr_HR.UTF-8
LC_MEASUREMENT=hr_HR.UTF-8
LC_IDENTIFICATION=hr_HR.UTF-8

It doesn’t help even if I place export LC_CTYPE="hr_HR.UTF-8" to my .bash_profile. My LC_CTYPE stays as C as an output of locale.

This bug is very frustrating, since every single typo with an accented character key in my command line, followed by backspace is punished with a “command not found” error.

Anyone else could verify this issue?

In the new line type exactly this (using “[” and “]” to mark separate keystrokes):

[č] [Backspace] [l] [s] [Enter]

If your keyboard doesn’t have the “č” character, you may copy-paste it from here.

Expacted behavior: Should execute the ls command.
What actually happens: Otputs the bash: $'\304ls': command not found error.

Just tried this – I’m using Manjaro Gnome with Gnome Terminal 3.44.1. It works fine on my system - typing the string of characters just gives me the output of the ls command - no errors or the like.

edit: oh, and my shell is zsh 5.9

1 Like

The link you shared was a wonderful trace, since that’s exactly what’s happening in my case: “a terminal (emulator) which understands multibyte encodings (UTF-8), but at the same time - the shell doesn’t”.

Unfortunately, I couldn’t resolve my issue with the solution proposed over there. My configs below:

$ localectl
   System Locale: LANG=en_US.UTF-8
                  LC_CTYPE=hr_HR.UTF-8
                  LC_NUMERIC=hr_HR.UTF-8
                  LC_TIME=hr_HR.UTF-8
                  LC_MONETARY=hr_HR.UTF-8
                  LC_PAPER=hr_HR.UTF-8
                  LC_NAME=hr_HR.UTF-8
                  LC_ADDRESS=hr_HR.UTF-8
                  LC_TELEPHONE=hr_HR.UTF-8
                  LC_MEASUREMENT=hr_HR.UTF-8
                  LC_IDENTIFICATION=hr_HR.UTF-8
       VC Keymap: croat
      X11 Layout: hr
       X11 Model: pc105
$ locale
LANG=hr_HR.UTF-8
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C
$ cat /etc/default/locale
LANG=en_US.UTF-8
LC_ADDRESS=hr_HR.UTF-8
LC_IDENTIFICATION=hr_HR.UTF-8
LC_MEASUREMENT=hr_HR.UTF-8
LC_MONETARY=hr_HR.UTF-8
LC_NAME=hr_HR.UTF-8
LC_NUMERIC=hr_HR.UTF-8
LC_PAPER=hr_HR.UTF-8
LC_TELEPHONE=hr_HR.UTF-8
LC_TIME=hr_HR.UTF-8
$ cat /etc/X11/xorg.conf.d/00-keyboard.conf
# Written by systemd-localed(8), read by systemd-localed and Xorg. It's
# probably wise not to edit this file manually. Use localectl(1) to
# instruct systemd-localed to update it.
Section "InputClass"
        Identifier "system-keyboard"
        MatchIsKeyboard "on"
        Option "XkbLayout" "hr"
        Option "XkbModel" "pc105"
EndSection
$ cat /etc/vconsole.conf
KEYMAP=croat
FONT=
FONT_MAP=

Any ideas on how to resolve this issue?