zsh-users / zsh-syntax-highlighting

Fish shell like syntax highlighting for Zsh.
github.com/zsh-users/zsh-syntax-highlighting
BSD 3-Clause "New" or "Revised" License
20k stars 1.33k forks source link

Syntax-highlighting of command will disappear after an unsuccessful completion #919

Open LuckyWindsck opened 1 year ago

LuckyWindsck commented 1 year ago

Unexpected behavior

After I input something and pressed tab key for completion, if the completion failed, the highlighting of command would disappear.

For example, I have installed this plugin through antigen, and installed Homebrew as well as its zsh completion.

First, I typed brew z

image

and then I pressed tab key for completion. Note that Homebrew doesn't have a subcommand started with z, so the completion will somehow fail. Now the syntax highlighting of command brew disappeared.

image

If I try something like brew i, then press tab, this time the completion will be successful. A completion list of subcommand started with i will be displayed, and the syntax-highlighting work as expected.

image

Workaround

I am using oh-my-zsh. I found that if I uncomment the following line in the default ~/.zshrc, the problem stated above will be solved.

# COMPLETION_WAITING_DOTS="true"

I have investigated the COMPLETION_WAITING_DOTS feature of oh-my-zsh, but I cannot find anything useful to figure out the cause of unexpected behavior.

Environment

Operating System: macOS 12.6.3 Zsh: zsh 5.8.1 (x86_64-apple-darwin21.0) zsh-users/zsh-syntax-highlighting: b2c910a85ed84cb7e5108e7cb3406a2e825a858f (commit hash) Antigen: v2.2.3 (ff391b5) Homebrew: 4.0.9

LuckyWindsck commented 1 year ago

I just found that this is a duplicate issue of #90 (as well as #102 and #113). However all of them are closed and this problem still exists. I am not really sure what happened.

LuckyWindsck commented 1 year ago

It seems like that the default zle widget of tab key ("^I") is expand-or-complete, and invoking the redisplay widget after expand-or-complete widget can solve this problem.

This is how Oh My Zsh implements the expand-or-complete-with-dots widget, and bind it to tab key ("^I").

So anyone who has similar problem can just use the workaround states above, or follow Oh My Zsh to define a zle widget like this:

expand-or-complete-then-redisplay() {
  zle expand-or-complete
  zle redisplay
}

zle -N expand-or-complete-then-redisplay

# Set the function as the default tab completion widget
bindkey "^I" expand-or-complete-with-dots
phy1729 commented 1 year ago

90 was closed by https://github.com/zsh-users/zsh-syntax-highlighting/commit/c14fcad3b08e19dce750b4b0eb6cc597e0f93d57 because highlighting does not disappear when using add-zle-hook-widget. However, we need the memo feature, so using add-zle-hook-widget is gated behind a version check for 5.9 or later. If you use the homebrew version of zsh, you shouldn't keep seeing this.

LuckyWindsck commented 1 year ago

@phy1729: If you use the homebrew version of zsh, you shouldn't keep seeing this.

I am using the default version of zsh provided by macOS, should I installed another zsh via homebrew and use that?

phy1729 commented 1 year ago

As long at you're using at least zsh 5.9, the add-zle-hook-widget way of running will be used and the issue should no longer appear.

LuckyWindsck commented 1 year ago

@phy1729: As long at you're using at least zsh 5.9, the add-zle-hook-widget way of running will be used and the issue should no longer appear.

I will try a newer version of zsh later. Thanks for your help. If this solve the problem, I will close this issue.

$ echo $SHELL     
/bin/zsh
$ $SHELL --version
zsh 5.8.1 (x86_64-apple-darwin21.0)