Weird behaviour/rendering on tmux in osx

8,029

I have seen a problem like this when using multi-byte (UTF-8 encoded) characters in the prompt, but only when tmux’s utf8 window option is turned off.

To identify such characters look for any “fancy” shapes, special symbols, or accented characters that are outside the ASCII repertoire (i.e. any Unicode codepoint beyond U+007F).

To check whether the utf8 option is active use this command in a window that is having the problem:

tmux show-option -gw utf8 \; show-option -w utf8

(or at a tmux : prompt without the leading tmux and without the backslash)

If it shows two values, then the second one is the active value for that window. The first (or only) value is the global value that will be used if there is no window-local value established.

If you are seeing a different value from the one established by your configuration file, then you probably need to restart your server (or iff the changes made by your configuration file are idempotent, then you can source it: tmux source ~/.tmux.conf).


The problem arises because (without utf8 enabled) tmux and your external terminal emulator have a different idea of how many columns are used to display each multibyte character. tmux expects a multibyte character to take up multiple columns (one for each byte), but your external emulator is probably configured to recognize it as a single UTF-8 character and thus will render it in a single column.

The problem is especially visible in a zsh prompt because zsh is careful to track where the cursor is so that is knows when the line will break and what it needs to do to properly redraw the prompt. Unfortunately, the mismatch between (non-utf8) tmux and the (UTF-8 configured) external emulator causes tmux to report a cursor position that is several columns to the right of the expected position (one extra space for each extra byte in every multibyte character in the prompt).

Enabling utf8 in tmux fixes the problem because both tmux and the external emulator will recognize the sequences for each multibyte UTF-8 codepoint as taking up a single column. Reconfiguring your external emulator to use a single-byte encoding would also fix the discrepancy, but at the cost of not being able to properly view UTF-8 encoded data (although such a view does basically show you what tmux is “thinking” when utf8 is off).

Share:
8,029

Related videos on Youtube

uorbe001
Author by

uorbe001

Updated on September 18, 2022

Comments

  • uorbe001
    uorbe001 over 1 year

    I've been having this issue for some time now, and it is really annoying, but I can't figure out what's wrong. When I start a tmux session on my terminal, no matter if it is on Terminal.app or iTerm2, I always get a weird space between the prompt and the text I type, roughly 3 times the space I get outside tmux. This wouldn't be a big issue by itself, but when I try to press backspace to delete something or use vi mode (zsh) to modify something, the cursor displays as if the text was in the right position (the way it displays outside tmux) but the text is on the wrong place, so it is impossible to know what I'm actually modifying. Also, when I press enter, I can see the text blink to the right position, but this isn't really useful at that point.

    I've tried reporting the $TERM type with different options when I start tmux, but none of them seem to make it better.

    Does anyone know what the issue could be?

    EDIT: This is my $PROMPT/$PS1: %{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}

    • Chris Johnsen
      Chris Johnsen over 10 years
      Do you have multibyte characters in your prompt? Is the utf8 window option turned on for your tmux windows?
    • Chris Johnsen
      Chris Johnsen over 10 years
      When looking for multibyte the prime suspects would be “fancy” shapes like arrows, triangles, accented characters, etc. If you output them to tmux when it has utf8 turned off, then its idea of the cursor position and the outer terminal’s idea of the cursor position will be different; zsh asks for the cursor position, so it is sensitive to this discrepancy (which shows up as extra spaces at the end of its prompt). You might want to edit your question to include your actual PROMPT/PS1 value.
    • uorbe001
      uorbe001 over 10 years
      @ChrisJohnsen I do have "fancy" shapes, I'm editing the original post to include my PROMPT value as suggested :)
    • Chris Johnsen
      Chris Johnsen over 10 years
      Are you sure you have not turned off utf8 for that window (despite having the global value be on)? I certainly see extra spaces when using your PROMPT value if I set utf8 off for the window. What does tmux show -w utf8 \; show -gw utf8 show in a window you are having problems in?
    • uorbe001
      uorbe001 over 10 years
      @ChrisJohnsen It seems you are right, it does seem to say it is off. I get utf8 off when I run that command. Something must me wrong on my tmux.conf, gotta find out what!
    • uorbe001
      uorbe001 over 10 years
      I found my problem! even though I had the utf8 setting on the tmux.conf, I didn't notice that I had some tmux sessions started, so it wasn't reloading the config. Killing all the tmux sessions made it reload the config and got rid of the annoying space. Thanks @ChrisJohnsen
    • DanielZ
      DanielZ over 9 years
      I've had the same problem, with the utf8 options both enabled (on) in tmux and iterm. However I was using tmux inside of zsh, and I needed to export my language environment to: export LANG=en_US.UTF-8.
  • uorbe001
    uorbe001 over 10 years
    In my case, I didn't notice I had some tmux sessions opened on the background by already closed shells, I found them by running tmux list-sessions and I simply killed the tmux process. Tmux only reloads the tmux.conf file when the server restarts, so my changes on the conf were not being loaded.
  • artfulrobot
    artfulrobot almost 10 years
    When I set utf8 in tmux, vim stops printing the correct utf8 characters! I seem to be able to (a) have vim display the correct characters but have lines wrapping and content all over the place, or (b) have layout fixed but not get the right characters!
  • Chris Johnsen
    Chris Johnsen almost 10 years
    @artfulrobot: You probably should start a new question, your situation will depend on your Vim settings (e.g. tenc, fenc, enc, etc.) and possibly your environment (e.g. LANG, LC_CTYPE, etc.).
  • Mario
    Mario almost 8 years
    Quick update: utf8 option is removed in newer versions of tmux