zsh-autosuggestions icon indicating copy to clipboard operation
zsh-autosuggestions copied to clipboard

The color of the automatic prompt is incorrect.

Open liby opened this issue 5 years ago • 23 comments

Describe the bug

After running the source .zshrc command, the color of the auto prompt is displayed incorrectly and is not grayed out.

To Reproduce

Steps to reproduce the behavior:

  1. Run source .zshrc command.
  2. Enter other commands to see tips

Expected behavior

The tips section should be gray.

Screenshots

  • Expected behavior Expected

  • Current behavior Current

demo

Desktop

  • OS + distribution: macOS 10.15.2 (19C57) && Darwin 19.2.0
  • Zsh version: 5.7.1
  • Plugin version: ae315ded4dba10685dbbafbfa2ff3c1aefeb490d

Additional context

Although running the exec zsh command will make it display properly, but this does not completely solve the problem.Every time I run source .zshrc, it reproduces the incorrect display.

`.zshrc` file content
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
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

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# Export Set
export ZSH="$HOME/.oh-my-zsh"
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"

# Base Set
COMPLETION_WAITING_DOTS="true"
ENABLE_CORRECTION="true"
# ZSH_DISABLE_COMPFIX="true"
ZSH_THEME="powerlevel10k/powerlevel10k"

# Plugins Set
plugins=(
  autojump
  git
  zsh-autosuggestions
  zsh-completions
  zsh-syntax-highlighting
)

autoload -U compinit && compinit
# This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Source Set
source $ZSH/oh-my-zsh.sh
# source ~/.bash_profile
if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile;
fi

[[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh

# Alias Set
alias ll='ls -alFh'
alias flushdns='sudo killall -HUP mDNSResponder'

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

Other issues

By the way, how can plug-ins like this installed through git be updated? The install.md only says how to install through git, but does not tell me how to update.

liby avatar May 26 '20 08:05 liby

Looks like a dup of https://github.com/zsh-users/zsh-autosuggestions/issues/516. Take a look there.

You can update plugins installed through git by running git pull (or any other git commands) in the cloned repo.

ericfreese avatar May 26 '20 16:05 ericfreese

@ericfreese Thank you for your reply. I thought that executing the upgrade_oh_my_zsh command would update all the plugins under the ~/.oh-my-zsh/custom/plugins path, but now it doesn't seem to be.

It was only when I created this issue yesterday that I updated the 'zsh-autosuggestions' plugin to the latest through the' git pull'', which was previously the code version in September 2019.

And a week ago, I didn't have this problem when I run source .zshrc. I don't know where the conflict is. But since I discovered the problem yesterday, whether it is the September 2019 version or the current latest version, there is this problem.

Now it seems that it can only be replaced by the exec zsh command. You can decide whether or not to close the question. Thank you again for your reply.

liby avatar May 27 '20 01:05 liby

I think I found out where the conflict is, When I was browsing #516, I find that we are all using powerlevel10k. I try to comment it out and then run source .zshrc and everything is fine.

@romkatv Maybe you can take a look at it for me. I'm really confused. I feel that replacing it with the exec zsh command is just a compromise and doesn't really solve the problem.

demo

liby avatar May 27 '20 01:05 liby

  1. Run source .zshrc command.

Don't. Use exec zsh if you want to reload zsh without starting a new process. It works better and takes fewer keystrokes.

romkatv avatar May 27 '20 04:05 romkatv

@romkatv Well, I'm sorry to bother you. Thank you. It's just that from a personal point of view, I'd like to know why they conflict.

liby avatar May 27 '20 07:05 liby

I'd like to know why they conflict.

I'm not aware of any conflict.

source ~/.zshrc evaluates ~/.zshrc for its side effects. If you run it twice, ~/.zshrc will be evaluated twice. The first evaluation starts from the clean slate. The second evaluation starts with the side effects from the first run already in place. In general anything with side effects is not idempotent, so in general evaluating ~/.zshrc twice is not equivalent to evaluating it once.

Instead of asking why running source ~/.zshrc sometimes breaks zsh, it's more appropriate to ask why would you expect it to work.

Another good question to ask is why you run source ~/.zshrc in the first place. If you want to restart zsh without creating a new process, use exec zsh. It's shorter, easier to type, and actually works.

romkatv avatar May 27 '20 07:05 romkatv

@romkatv

Did you notice the gif picture I sent above?

You can obviously see that if powerlevel10k is not enabled, running source ~/.zshrc does not cause any side effects with the 'zsh-autosuggestions' plugin. Everything looks normal.

But if powerlevel10k is enabled, then running the source ~/.zshrc command will cause the 'zsh-autosuggestions' plugin to display incorrectly. The color of the automatic prompt should have been gray.

If powerlevel10k is not enabled, no matter how many times you run source ~/.zshrc, it will not cause problems with the display of the' zsh-autosuggestions' plugin. So I said there was a conflict.

liby avatar May 27 '20 07:05 liby

@romkatv

Did you notice the gif picture I sent above?

Yes.

You can obviously see that if powerlevel10k is not enabled, running source ~/.zshrc does not cause any side effects with the 'zsh-autosuggestions' plugin. Everything looks normal.

When you run source ~/.zshrc, pretty much anything can happen. Sometimes this "anything" is nothing. Or it can appear to be nothing. Maybe your shell is a bit broken. Maybe it's a bit slower.

But if powerlevel10k is enabled, then running the source ~/.zshrc command will cause the 'zsh-autosuggestions' plugin to display incorrectly.

Anything can happen.

The color of the automatic prompt should have been gray.

No. Anything goes.

If powerlevel10k is not enabled, no matter how many times you run source ~/.zshrc, it will not cause problems with the display of the' zsh-autosuggestions' plugin.

OK.

So I said there was a conflict.

You are incorrect.

Here's an analogy.

  • Problem: when I shoot myself in the foot, it hurts.
  • Solution: don't.

If there was any reason for you to run source ~/.zshrc, then saying "don't" wouldn't be appropriate. There is no reason though, so just don't. You'll lose nothing if you stop running this command.

romkatv avatar May 27 '20 08:05 romkatv

@romkatv Maybe I'm a little obsessive-compulsive. Maybe our understanding of things seems to be fundamentally different.

You think there is a hidden danger in source ~/.zshrc, even if you can't see it now, you don't know when it will break out. So you think the best way is not to use it, and there's no reason to use it.

For the plugin 'zsh-autosuggestions' alone, what I care about is:

  1. Not Using powerlevel10k, running source ~/.zshrc is in good condition. Without discussing other hidden dangers, there is no problem with its use in terms of display alone.
  2. After enabling powerlevel10kand running source ~/.zshrc, the plug-in did not work as expected.

All I care about is why we don't have this obvious problem with the 'zsh-autosuggestions' plugin' if we don't enable powerlevel10k and run source ~/.zshrc.

Why when powerlevel10k is enabled, running source ~/.zshrc will cause the' zsh-autosuggestions' plugin to repeat the same problem over and over again. And I'm not the only one with the same problem.

We will not discuss other problems and hidden dangers of source ~/.zshrc here. The problem I see is that if powerlevel10k is not enabled, the 'zsh-autosuggestions' plugin will display normally after running source ~/.zshrc.

When powerlevel10k is enabled, running source ~/.zshrc, will cause the same problem with the 'zsh-autosuggestions' plugin'.

As expected, the rest of the commands it prompts should be light gray. But it shows that it is white. This problem can be stably reproduced. Then I think we should find out why.

And the reason is also found by the method of controlling variables, that is, with or without powerlevel10k enabled.

So I think it is irresponsible to simply and rudely attribute the problem to the source ~/.zshrc command.

liby avatar May 27 '20 09:05 liby

I think I've provided both the theoretical and the practical answer to your question.

  • Theoretical question: Isn't it wrong that X happens when I run source ~/.zshrc?
  • Answer: No, it's not wrong. If you are using third-party code in ~/.zshrc, anything can happen when you run source ~/.zsrhc. There are no guarantees and no expectations. If you have expectations, you should forget about them.
  • Practical question: How do I reload zsh after changing configuration?
  • Answer: Run exec zsh.

If you could follow up by saying that exec zsh doesn't solve your practical problem, that would be one thing. But I don't see you making that argument.

So I think it is irresponsible to simply and rudely attribute the problem to the source ~/.zshrc command.

I'm sorry if you've found my direct answer rude. My goal is to be unambiguous. You are running source ~/.zshrc when you want to reload zsh. This goal can be achieved better by exec zsh. source ~/.zshrc is not supposed or meant to reload zsh and it doesn't reload zsh. Thus it shouldn't be used to reload zsh.

romkatv avatar May 27 '20 09:05 romkatv

@romkatv Well, I guess I see what you mean. What I'm obsessed with is why there is such a problem, and you think it's nothing to worry about, and it's not surprising that it's so bad that it leads to any problems.

liby avatar May 27 '20 09:05 liby

Indeed. Shooting yourself in the foot can cause problems. One could be curious under which conditions this pointless and dangerous exercise is less or more painful. Investigating this isn't productive use of anyone's time though.

romkatv avatar May 27 '20 09:05 romkatv

I think it is a problem of terminal, try to export TERM=xterm-256color. I have solved this problem by this way.

warren-lei avatar Jul 23 '21 05:07 warren-lei

@warren-lei

I tried. It didn't solve the problem. I don't know if you can tell the difference.

https://user-images.githubusercontent.com/38807139/126740559-4ef0e07f-19ee-489b-8e9b-70d63f54969a.mp4

liby avatar Jul 23 '21 05:07 liby

#516 worked for me. I use zsh-syntax-highlighting and zsh-autosuggestions at the same time. I had the same issue, except that it happened all the time irrespective of whether I used exec zsh or source ~/.zshrc, but it worked normally after I commented out the highlighting plugin. It turns out that the plugin have not been updated for quite a long time. The problem was gone after I updated zsh-syntax-highlighting.

Chattille avatar Feb 07 '22 10:02 Chattille

#516 worked for me. I use zsh-syntax-highlighting and zsh-autosuggestions at the same time. I had the same issue, except that it happened all the time irrespective of whether I used exec zsh or source ~/.zshrc, but it worked normally after I commented out the highlighting plugin. It turns out that the plugin have not been updated for quite a long time. The problem was gone after I updated zsh-syntax-highlighting.

This was my case, thanks for posting the solution :+1:

gfsd3v avatar Feb 17 '22 13:02 gfsd3v

Oh, this issue is so long ago that I have forgotten the specifics of the time. When I saw @Chattille reply, I really thought the problem was solved (maybe). In fact, in the beginning of the discussion, we have already tried the method of updating the zsh-syntax-highlighting plugin, which is the solution mentioned in #516. The result is that if you use p10k, this solution does not work and you can only use the exec zsh command instead of source ~/.zshrc.

TL;DR

https://user-images.githubusercontent.com/38807139/155459595-fa3468b4-b3ee-43cc-85ca-c86fb6fe27c4.mp4

liby avatar Feb 24 '22 04:02 liby

When I saw @Chattille reply, I really thought the problem was solved (maybe).

As far as I'm concerned it was solved.

The result is that if you use p10k, this solution does not work and you can only use the exec zsh command instead of source ~/.zshrc.

The solution is to never run source ~/.zshrc. It doesn't matter whether you are using p10k or not.

TL;DR

The TL;DR is to not run source ~/.zshrc. Use exec zsh instead.

romkatv avatar Feb 24 '22 07:02 romkatv

Now I solved this by updating the autosuggestion plugin, but quite discussion here. Since exec zsh seems to be the proper way to source things, can you detail how it would look to source P10k?

In my .zshrc file when sourcing p10k config file should it be exec patch/to/config/file ?

tnfru avatar May 02 '22 10:05 tnfru

Since exec zsh seems to be the proper way to source things, can you detail how it would look to source P10k?

In my .zshrc file when sourcing p10k config file should it be exec patch/to/config/file ?

Put source commands in ~/.zshrc to source files. Use exec zsh to restart zsh after making changes to ~/.zshrc. You can also close your terminal and reopen it instead of running exec zsh.

romkatv avatar May 17 '22 08:05 romkatv

I also have an issue with the color, I also have p10k, but the issue is still there when I disable p10k. My highlight is red to make the problem more evident. As I type, the nightlight disappear and reappear and I can't seem to figure out why.

Here are my plugins:

plugins=(
    zsh-history-substring-search
    zsh-autosuggestions
    git
    git-open
    docker
    nvm
    npm
    yarn
)
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=9"
ZSH_AUTOSUGGEST_STRATEGY=(history completion)
bindkey '^[OC'   vi-forward-blank-word

2024-02-28 16 45 00

mrleblanc101 avatar Feb 28 '24 21:02 mrleblanc101

This issue is when I enable zsh-history-substring-search, I'll post the solution if I find it

mrleblanc101 avatar Feb 28 '24 21:02 mrleblanc101

I have added this to my zshrc:

bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

Not sure why this was required as this seems to be the default

mrleblanc101 avatar Feb 29 '24 05:02 mrleblanc101