Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

percent (%) character displayed before prompt when zsh is used #778

Open
M4he opened this issue Dec 28, 2020 · 11 comments
Open

percent (%) character displayed before prompt when zsh is used #778

M4he opened this issue Dec 28, 2020 · 11 comments
Labels

Comments

@M4he
Copy link

M4he commented Dec 28, 2020

Expected Behavior
  • the terminal prompt should be displayed without any preceding newlines or characters and behave like in other terminal emulators (konsole, gnome-terminal, tilix etc.)
  • qterminal should support zsh and oh-my-zsh configurations properly
Current Behavior
  • when opening a new qterminal instance while having zsh set as the default shell, a color-inverted percent sign (%) will displayed above the prompt
    debian-xfce-qterminal-zsh-bug
  • note: on Debian 10 buster, this happens with plain zsh and prompt adam1 set in ~/.zshrc
  • note: on Ubuntu 20.04, this happens with zsh as default shell and oh-my-zsh set up
    • in contrast to Debian, on Ubuntu it only occasionally happens without oh-my-zsh
Possible Solution

I have found the following two end-user workarounds (not solutions) so far. Both with downsides. The permanent solution would be to fix whatever is causing this in qterminal directly.

  • Workaround 1

    • append to the end of ~/.zshrc:
      unsetopt PROMPT_SP
      
    • downsides:
      • this will lead to broken newline behavior, see here
  • Workaround 2

    • append to the end of ~/.zshrc:
      setopt PROMPT_CR
      setopt PROMPT_SP
      export PROMPT_EOL_MARK=""
      
    • downsides:
      • makes the % disappear but an empty line will still be displayed above prompts in new qterminal instances
Steps to Reproduce (for bugs)
  1. Boot up a Debian 10 Xfce live image in a virtual machine (*-xfce.iso from here). All following instructions refer to the live system.
  2. In the live system, open up a terminal and:
    sudo apt update
    sudo apt install qterminal zsh
    chsh -s /bin/zsh   # (the password for the live user is "live")
    
  3. Create a file called .zshrc in the current home directory (~/.zshrc) with the following contents:
    autoload -Uz compinit promptinit
    compinit
    promptinit
    prompt adam1
    
  4. Log out ("Applications" menu -> "Log Out") and back in (username is user, password is live). The command echo $SHELL should now display /bin/zsh.
  5. Open up qterminal. The issue should appear. If not, try closing and re-opening a few times.
Context

The zsh shell is often used in conjunction with oh-my-zsh to provide convenient theming and extension management and configuration for zsh. As of now, it doesn't seem possible to use such zsh configuration with qterminal without having buggy behavior of the prompt in new terminal instances (erroneous newlines or % characters displayed above the prompt).

It would be great if qterminal could be compatible with zsh configurations the same way that all other popular terminal emulators are.

System Information
  • Distribution & Version: Debian 10 buster
  • Kernel: 4.19.0-13
  • Qt Version: 5.11.3
  • qtermwidget Version: 0.14.1
  • lxqt-build-tools Version: 0.6.0 (in repositories, not installed)
  • Package version: 0.14.1
Notes and References

Notes:

  • the issue also appears on real-world Debian installations, the live image used above is just for easier reproducibility of the issue
  • when setting prompt walters in ~/.zshrc on the Debian setup without oh-my-zsh, the issue seems to disappear
  • on Ubuntu 20.04, just using zsh with the config above will only rarely make the issue appear; however, installing oh-my-zsh also seems to make it reproducible on Ubuntu

References:

@yan12125 yan12125 added the bug label Dec 28, 2020
@yan12125
Copy link
Member

Thanks for this great report! As we've previously discussed on IRC, this is a valid bug report even the reporter is using some very old qterminal as I'm able to reproduce the issue on Arch Linux with the latest git-master.

on Ubuntu it only occasionally happens without oh-my-zsh

That is a useful information. I moved away from oh-my-zsh some time ago. It might be the reason that I seldom see such an issue recently.

@ssliman
Copy link

ssliman commented Mar 6, 2021

@M4he

Go inside the .zshrc file at the top you'll find:

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH <- uncomment this line

That's what worked for me

@M4he
Copy link
Author

M4he commented Mar 6, 2021

@xZliman

I tried adding your quoted line¹ (uncommented) to the very top of my ~/.zhsrc file but it doesn't make a difference for me.

¹ I did not have the comment in mine, because my file was based on an older template apparently.

@gitzhangzhao
Copy link

gitzhangzhao commented Jun 9, 2021

@M4he
I also encountered this problem, I use oh-my-zsh and powerlevel10k, I found that powerlevel10k instant prompt feature caused this problem:
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" fi
Adding this statement at the top of the .zshrc prints a reflected % on the first line of the newly opened terminal, like this:
2021-06-09_22-14
I tracked the start-up of the zsh, but in the end I didn't find the cause of the problem, but it must have been caused by powerlevel10k's instant prompt, my simple solution is to discard all the output:
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null fi
This really solved my problem.
2021-06-09_22-18
I hope this will help you.

@EigenTheory
Copy link

@M4he
I also encountered this problem, I use oh-my-zsh and powerlevel10k, I found that powerlevel10k instant prompt feature caused this problem:
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" fi
Adding this statement at the top of the .zshrc prints a reflected % on the first line of the newly opened terminal, like this:
2021-06-09_22-14
I tracked the start-up of the zsh, but in the end I didn't find the cause of the problem, but it must have been caused by powerlevel10k's instant prompt, my simple solution is to discard all the output:
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null fi
This really solved my problem.
2021-06-09_22-18
I hope this will help you.

This worked great for me and I tried all other solutions! This was the only solution that provided results.

@flaviocamilo
Copy link

@M4he
I also encountered this problem, I use oh-my-zsh and powerlevel10k, I found that powerlevel10k instant prompt feature caused this problem:
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" fi
Adding this statement at the top of the .zshrc prints a reflected % on the first line of the newly opened terminal, like this:
2021-06-09_22-14
I tracked the start-up of the zsh, but in the end I didn't find the cause of the problem, but it must have been caused by powerlevel10k's instant prompt, my simple solution is to discard all the output:
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null fi
This really solved my problem.
2021-06-09_22-18
I hope this will help you.

I can reproduce this solution.

Thanks!

@romkatv
Copy link

romkatv commented Aug 28, 2021

Hey everyone. I'm the developer of powerlevel10k. @flaviocamilo has tagged me on romkatv/powerlevel10k#1445.

When you see the inverted percent before the first prompt upon opening a new terminal, this is almost always caused by a bug in the terminal or the window manager (wm). You can diagnose the nature of the bug with the following steps.

First, add this snippet at the very top of ~/.zshrc:

PS1='%~ '
RPS1='%n@%m'
return

This sets left and right prompt to very simple values and returns before evaluating anything else in your zsh config. If you can reproduce the problem with this .zshrc (that is, you still occasionally get the inverted percent sign upon opening a new terminal), this means the issue is not with your zsh theme or your zsh config in general.

The next step is to figure out which of the two common terminal/wm bugs is the culprit. The most common variety is that the TTY reports its size incorrectly for a short while after being created. To see whether this is the case add the following snippet at the very top of ~/.zshrc:

print -r -- "$COLUMNS x $LINES"
sleep 1
print -r -- "$COLUMNS x $LINES"

When you open a new terminal, you should see the same dimensions printed twice and they should match the real dimensions of your terminal window. If you see different numbers reported on the first and the second line, or if they don't match the real dimensions of the terminal window, this is an indication of the bug I was referring to earlier.

If you always see correct dimensions printed twice, check for another terminal/wm bug that can cause the inverted percent sign. This bug involves incorrect line wrapping during a short duration after the TTY is created. To check for it, add the following snippet at the very top of ~/.zshrc:

() {
  emulate -L zsh -o prompt_percent
  local -i n='COLUMNS - 1'
  print -Prn -- X${(pl.$n.. .)}$'\r%E'
}

When you open a new terminal, you should NOT see "X" at the top of your terminal window. If you do see it, that's a bug in the terminal/wm.

Now, why do you see the inverted percent sign when using powerlevel10k instant prompt but not when using other themes? This is simply because other themes are unable to display prompt fast enough. Powerlevel10k displays prompt very quickly after you open a terminal window, so it can be affected by a terminal/wm bug that only manifests itself for a short duration after a TTY is created.

my simple solution is to discard all the output:

if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null
fi

By redirecting the output to /dev/null you've disabled instant prompt. If you really want to disable instant prompt, simply delete this whole snippet. It'll have the same effect (no instant prompt) and will be simpler and faster. Alternatively, you can leave the original instant prompt snippet unchanged and set POWERLEVEL9K_INSTANT_PROMPT=off in ~/.p10k.zsh (search that file for "POWERLEVEL9K_INSTANT_PROMPT" -- this parameter is already present there but with a different value). You can also do this by running p10k configure and selecting "Off" when asked what you want w.r.t. instant prompt.

These are just workarounds though. Ideally the bug needs to be fixed in the terminal or the window manager.

@gitzhangzhao
Copy link

sleep x reduces the frequency of % occurrence while retaining instant prompt, add sleep 0.1 at the top of ~/.zshrc. Sleep time can be tried by yourself. For me, if the sleep time is between 0.05 and 0.1, about 15 zsh starts will have 1 %. The benefit is that the shell starts faster than simply disable instant prompt.

@romkatv
Copy link

romkatv commented Aug 31, 2021

@gitzhangzhao Could you try it with the latest version of Powerlevel10k? I’ve implemented a partial workaround that might get rid of the inverted percent sign even without the sleep.

@gitzhangzhao
Copy link

@gitzhangzhao Could you try it with the latest version of Powerlevel10k? I’ve implemented a partial workaround that might get rid of the inverted percent sign even without the sleep.

@romkatv It worked, thank you very much!

@romkatv
Copy link

romkatv commented Aug 31, 2021

@gitzhangzhao Thanks for verifying!

It's really just a workaround though. As I mentioned earlier, this is a bug either in the terminal or in the window manager. The bug is not specific to powerlevel10k and can easily be reproduced with a tiny zsh config. It's not specific to zsh either, which can be demonstrated by instructing the terminal to launch stty size instead of the login shell. You should see that the output of stty size doesn't match the real terminal dimensions when you open a new terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants