zsh-syntax-highlighting: syntax highlighting not working with zsh 5.9 and zsh-vi-mode

After updating to the latest zsh version. The zsh-syntax-highlighting does not work anymore.

OS: Archlinux
Version: zsh 5.9 (x86_64-pc-linux-gnu)

Related Config:

plugins=(
    zsh-autosuggestions
    zsh-syntax-highlighting
    # vi-mode
    zsh-vi-mode
    git
    # forgit
    # sudo
    # macos
    web-search
    # copydir
    # copyfile
    # copybuffer
    dirhistory
    colored-man-pages
    # vscode
    # xcode
)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 27 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Perhaps the following? I don’t have an environment to test it in:

diff --git a/zsh-vi-mode.zsh b/zsh-vi-mode.zsh
index c8cce0b..abea50f 100644
--- a/zsh-vi-mode.zsh
+++ b/zsh-vi-mode.zsh
@@ -3253,7 +3253,8 @@ function zvm_init() {
   zvm_define_widget zvm_switch_keyword
 
   # Override standard widgets
-  zvm_define_widget zle-line-pre-redraw zvm_zle-line-pre-redraw
+  autoload add-zle-hook-widget
+  add-zle-hook-widget zle-line-pre-redraw zvm_zle-line-pre-redraw
 
   # Ensure the correct cursor style when an interactive program
   # (e.g. vim, bash, etc.) starts and exits

I’m also running zsh 5.9. For me syntax highlighting works fine until I add zsh-vi-mode to the plugins list in my zshrc. (Using oh-my-zsh plugins). Before zsh 5.9 both zsh-syntax-highlighting and zsh-vi-mode worked like a charm together.

I’m using the latest commits for both plugins, and am experiencing the same bug. zsh-syntax-highlighting = caa749d zsh-vi-mode = 9e909d0

If I set the zsh-vi-mode option, ZVM_INIT_MODE='sourcing', then syntax highlighting works again.

Hopefully this help debug.

Here is an alternative for zsh-syntax-highlight. It works fine on my machine.

https://github.com/zdharma-continuum/fast-syntax-highlighting

I guess the issue is casued by the deprecated zsh funcitons in version 5.9. There is a compatability section in 5.9 release note.

Perhaps the following? I don’t have an environment to test it in:

diff --git a/zsh-vi-mode.zsh b/zsh-vi-mode.zsh
index c8cce0b..abea50f 100644
--- a/zsh-vi-mode.zsh
+++ b/zsh-vi-mode.zsh
@@ -3253,7 +3253,8 @@ function zvm_init() {
   zvm_define_widget zvm_switch_keyword
 
   # Override standard widgets
-  zvm_define_widget zle-line-pre-redraw zvm_zle-line-pre-redraw
+  autoload add-zle-hook-widget
+  add-zle-hook-widget zle-line-pre-redraw zvm_zle-line-pre-redraw
 
   # Ensure the correct cursor style when an interactive program
   # (e.g. vim, bash, etc.) starts and exits

Thank you very much!! That solved the problem. Now I can use zsh-syntax-highlighting and zsh-vi-mode together without any problem.

Perhaps the following? I don’t have an environment to test it in:

diff --git a/zsh-vi-mode.zsh b/zsh-vi-mode.zsh
index c8cce0b..abea50f 100644
--- a/zsh-vi-mode.zsh
+++ b/zsh-vi-mode.zsh
@@ -3253,7 +3253,8 @@ function zvm_init() {
   zvm_define_widget zvm_switch_keyword
 
   # Override standard widgets
-  zvm_define_widget zle-line-pre-redraw zvm_zle-line-pre-redraw
+  autoload add-zle-hook-widget
+  add-zle-hook-widget zle-line-pre-redraw zvm_zle-line-pre-redraw
 
   # Ensure the correct cursor style when an interactive program
   # (e.g. vim, bash, etc.) starts and exits

It seems to work for me.

This fixes the issue for me too,

Perhaps the following? I don’t have an environment to test it in:

diff --git a/zsh-vi-mode.zsh b/zsh-vi-mode.zsh
index c8cce0b..abea50f 100644
--- a/zsh-vi-mode.zsh
+++ b/zsh-vi-mode.zsh
@@ -3253,7 +3253,8 @@ function zvm_init() {
   zvm_define_widget zvm_switch_keyword
 
   # Override standard widgets
-  zvm_define_widget zle-line-pre-redraw zvm_zle-line-pre-redraw
+  autoload add-zle-hook-widget
+  add-zle-hook-widget zle-line-pre-redraw zvm_zle-line-pre-redraw
 
   # Ensure the correct cursor style when an interactive program
   # (e.g. vim, bash, etc.) starts and exits

It seems to work for me.

@kstolp Thanks a lot.

I’m going to tentatively assume that everyone here uses the zsh-vi-mode plugin. (The third-party plugin linked above; not merely the builtin bindkey -v functionality.) If that’s not the case for anyone, speak up.

I suspect the problem has to do with how the zsh-vi-mode plugin’s use of zle-line-pre-redraw, since that widget is used by default by z-sy-h under zsh 5.9 but not (by default) under older zsh’s.

There seem to be several problems with the zsh-vi-mode plugin’s approach:

  1. zvm_define_widget() lacks ${(q)}'s on its eval.
  2. zvm_zle-line-pre-redraw() doesn’t inspect $rawfunc (set by zvm_widget_wrapper(), which zvm_define_widget() arranges to be called), so any pre-redraw hooks installed prior to zsh-vi-mode aren’t run at all. (They probably shouldn’t fix that by changing those functions I just named but by removing those functions altogether and using add-zle-hook-widget instead.)
  3. That also goes for the zsh-vi-mode plugin’s zle-line-init and zle-line-finish wrappers, at least.
  4. zvm_zle-line-pre-redraw() calls zvm_update_highlight() which, if I’m reading its code correctly, could remove some $region_highlight entries from plugins other than itself. (Note that multiple highlights can sometimes combine; e.g., region_highlight=("0 5 fg=red" "0 5 bold,underline").) It should use the memo= feature of zsh 5.9 to only remove its own entries. (Note that z-sy-h doesn’t use redrawhook on zsh versions that lack the memo= feature precisely in order to avoid breaking other plugins.)

The most important of these is (2) which, if I’m reading the zsh-vi-mode plugin’s correctly, causes that plugin to trample on plugins installed prior to itself.

I don’t see anything z-sy-h can do about a plugin that runs after z-sy-h’s init code and arranges for z-sy-h’s add-zle-hook-widget-registered hook not to be called.

/cc @jeffreytse

Triage: issue is in another codebase; nothing we can do on our end to fix it or to work around it; patch for the other project has been devised, tested, and PR’d to that project; thus: closing as “nothing further for us to do”. The issue is still reproducible with latest z-sy-h and latest zsh-vi-mode, true; if that affects you, apply the above patch to your copy of zsh-vi-mode.

@fbearoff Thanks for submitting the patch upstream as a PR!

Same issue. Also on Arch. Updated zsh to 5.9 a few hours ago. Not using ohmyzsh or anything. Haven’t changed anything config related. Also using dracula zsh-syntax-highlighting theme

ZSH_HIGHLIGHT_HIGHLIGHTERS=(main cursor)
typeset -gA ZSH_HIGHLIGHT_STYLES

ZSH_HIGHLIGHT_STYLES[builtin]='fg=#8BE9FD'
ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=#8BE9FD'
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=#8BE9FD'

ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=#FF79C6'
ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter-unquoted]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[process-substitution-delimiter]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument-delimiter]='fg=#FF79C6'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=#FF79C6'
ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]='fg=#FF79C6'

ZSH_HIGHLIGHT_STYLES[command-substitution-quoted]='fg=#F1FA8C'
ZSH_HIGHLIGHT_STYLES[command-substitution-delimiter-quoted]='fg=#F1FA8C'
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=#F1FA8C'
ZSH_HIGHLIGHT_STYLES[single-quoted-argument-unclosed]='fg=#FF5555'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=#F1FA8C'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument-unclosed]='fg=#FF5555'
ZSH_HIGHLIGHT_STYLES[rc-quote]='fg=#F1FA8C'

ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument-unclosed]='fg=#FF5555'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[assign]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[named-fd]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[numeric-fd]='fg=#F8F8F2'

ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=#FF5555'
ZSH_HIGHLIGHT_STYLES[path]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[path_pathseparator]='fg=#FF79C6'
ZSH_HIGHLIGHT_STYLES[path_prefix]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]='fg=#FF79C6'
ZSH_HIGHLIGHT_STYLES[globbing]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=#BD93F9'
#ZSH_HIGHLIGHT_STYLES[command-substitution]='fg=?'
#ZSH_HIGHLIGHT_STYLES[command-substitution-unquoted]='fg=?'
#ZSH_HIGHLIGHT_STYLES[process-substitution]='fg=?'
#ZSH_HIGHLIGHT_STYLES[arithmetic-expansion]='fg=?'
ZSH_HIGHLIGHT_STYLES[back-quoted-argument-unclosed]='fg=#FF5555'
ZSH_HIGHLIGHT_STYLES[redirection]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[arg0]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[default]='fg=#F8F8F2'
ZSH_HIGHLIGHT_STYLES[cursor]='fg=#FFFFFF'

source $HOME/.config/zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh