29

The first two chars were repeated while I use Tab to do completion. In the screenshot below, cd is repeated.

enter image description here

I have tried rxvt-unicdoe, xterm, terminator. All these terminal emulators have this issue.

Zsh version 5.0.2, config file on-my-zsh

2
  • Are the characters repeated in the command that zsh executes, or are they merely displayed? Does the number of characters change if the command is longer than two characters? Does the number change when the current directory changes? Sep 15, 2013 at 23:02
  • @Gilles The repeated char not exists in the command. I can execute command.
    – jilen
    Sep 16, 2013 at 11:19

4 Answers 4

42

If the characters on your command line are sometimes displayed at an offset, this is often because zsh has computed the wrong width for the prompt. The symptoms are that the display looks fine as long as you're adding characters or moving character by character but becomes garbled (with some characters appearing further right than they should) when you use other commands that move the cursor (Home, completion, etc.) or when the command overlaps a second line.

Zsh needs to know the width of the prompt in order to know where the characters of the command are placed. It assumes that each character occupies one position unless told otherwise.

One possibility is that your prompt contains escape sequences which are not properly delimited. Escape sequences that change the color or other formatting aspects of the text, or that change the window title or other effects, have zero width. They need to be included within a percent-braces construct %{…%}. More generally, an escape sequence like %42{…%} tells zsh to assume that what is inside the braces is 42 characters wide.

So check your prompt settings (PS1, PROMPT, or the variables that they reference) and make sure that all escape sequences (such as \e[…m to change text attributes — note that it may be present via some variable like $fg[red]) are inside %{…%}. Since you're using oh-my-zsh, check both your own settings and the definitions that you're using from oh-my-zsh.

The same issue arises in bash. There zero-width sequences in a prompt need to be enclosed in \[…\].

Another possibility is that your prompt contains non-ASCII characters and that zsh (or any other application) and your terminal have a different idea of how wide they are. This can happen if there is a mismatch between the encoding of your terminal and the encoding that is declared in the shell, and the two encodings result in different widths for certain byte sequences. Typically you might run into this issue when using a non-Unicode terminal but declaring a Unicode locale or vice versa.

Applications rely on environment variables to know the locale; the relevant setting is LC_CTYPE, which is determined from the environment variables LANGUAGE, LC_ALL, LC_CTYPE and LANG (the first of these that is set applies). The command locale | grep LC_CTYPE tells you your current setting. Usually the best way to avoid locale issues is to let the terminal emulator set LC_CTYPE, since it knows what encoding it expects; but if that's not working for you, make sure to set LC_CTYPE.

The same symptoms can occur when the previous command displayed some output that didn't end in a newline, so that the prompt is displayed in the middle of the line but the shell doesn't realize that. In this case that would only happen after running such a command, not persistently.

If a line isn't displayed properly, the command redisplay or clear-screen (bound to Ctrl+L by default) will fix it.

8
  • I guess I am probably missing related font, I notice the first char is strange. It is expected to be -> I think
    – jilen
    Sep 17, 2013 at 15:44
  • @jilen Ah, this could be another issue that I forgot to mention: maybe your prompt contains non-ASCII characters in a different encoding from your terminal, with one or both of the encodings being multibyte. If you want help with that, post the output of locale and of echo $PS1 | od -t x1 (and the same thing with any other variable used by $PS1). Sep 17, 2013 at 15:48
  • 2
    I forgot to set the locale(I am using archlinux, locale are left not set by default) . After set locale , this problem are fixed. Thanks a lot, guy!!!!
    – jilen
    Sep 17, 2013 at 16:20
  • I upvoted it because, well, it's pretty awesome. But escapes needn't absolutely be included in the brackets if you otherwise handle the cursor count on your own. calling a subshelled function has worked for me in the past - or redirections that still land on /dev/tty without involving stdout might work. Other methods that have worked - using \e{7,8} to save/restore cursor states.
    – mikeserv
    Jul 26, 2014 at 8:21
  • 1
    It was the LC_CTYPE that fixed it for me. I had it set to C, when I unset it, everything worked. Thanks.
    – jmaloney
    Oct 29, 2014 at 14:20
19

I had the same problem and found my solution here: https://github.com/robbyrussell/oh-my-zsh/issues/5157#issuecomment-226031519. Simply put the following into your ~/.zshrc.

export LC_CTYPE=en_US.UTF-8

3
  • 2
    Thanks for your hint! I, however, had to reset the whole locale i was using, but it worked for me. This answer provides the necessary steps for Ubuntu.
    – max
    Oct 10, 2016 at 13:22
  • Thank you so much! Saved me alot of aggrevation
    – Moshe
    Dec 20, 2018 at 8:50
  • Didn't work here. Apr 27, 2020 at 9:14
3

I had this issue in iTerm 2 on macOS. I ended up solving it by going to Preferences -> Profiles -> Text and ticking off "Use Unicode Version 9 Widths".

2
  • Wow, this actually worked. Thanks! May 22, 2019 at 4:26
  • Same, although the reverse worked for me. Apr 30, 2020 at 21:40
1

I've got this issue using ubuntu lts docker image (ubuntu:latest). I fixed it with instruction provided on the corresponding page: https://hub.docker.com/_/ubuntu

apt-get update && \
apt-get install -y locales && \
rm -rf /var/lib/apt/lists/* && \
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
echo 'export LANG=en_US.utf8' >> ~/.zshrc
zsh

You must log in to answer this question.

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