Skip to content

Instantly share code, notes, and snippets.

@nicoulaj
Created September 19, 2010 11:43
Show Gist options
  • Select an option

  • Save nicoulaj/586698 to your computer and use it in GitHub Desktop.

Select an option

Save nicoulaj/586698 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# ------------------------------------------------------------------------------
# Fish style live command coloring.
# ------------------------------------------------------------------------------
# Token types styles.
# See http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#SEC135
ZLE_RESERVED_WORD_STYLE='bold'
ZLE_ALIAS_STYLE='bold'
ZLE_BUILTIN_STYLE='bold'
ZLE_FUNCTION_STYLE='bold'
ZLE_COMMAND_STYLE='bold'
ZLE_COMMAND_UNKNOWN_TOKEN_STYLE='fg=red,bold'
ZLE_TOKENS_FOLLOWED_BY_COMMANDS=('|' '||' ';' '&' '&&' 'sudo' 'start' 'time' 'strace')
# Recolorize the current ZLE buffer.
colorize-zle-buffer() {
region_highlight=()
colorize=true
start_pos=0
for arg in ${(z)BUFFER}; do
((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]## #}}))
((end_pos=$start_pos+${#arg}))
if $colorize; then
colorize=false
res=$(LC_ALL=C builtin type $arg 2>/dev/null)
case $res in
*'reserved word'*) style=$ZLE_RESERVED_WORD_STYLE;;
*'an alias'*) style=$ZLE_ALIAS_STYLE;;
*'shell builtin'*) style=$ZLE_BUILTIN_STYLE;;
*'shell function'*) style=$ZLE_FUNCTION_STYLE;;
*"$cmd is"*) style=$ZLE_COMMAND_STYLE;;
*) style=$ZLE_COMMAND_UNKNOWN_TOKEN_STYLE;;
esac
region_highlight+=("$start_pos $end_pos $style")
fi
[[ ${${ZLE_TOKENS_FOLLOWED_BY_COMMANDS[(r)${arg//|/\|}]}:+yes} = 'yes' ]] && colorize=true
start_pos=$end_pos
done
}
# Bind the function to ZLE events.
colorize-hook-self-insert() { builtin zle .self-insert && colorize-zle-buffer }
colorize-hook-backward-delete-char() { builtin zle .backward-delete-char && colorize-zle-buffer }
colorize-hook-vi-backward-delete-char() { builtin zle .vi-backward-delete-char && colorize-zle-buffer }
zle -N self-insert colorize-hook-self-insert
zle -N backward-delete-char colorize-hook-backward-delete-char
zle -N vi-backward-delete-char colorize-hook-vi-backward-delete-char
@roylez
Copy link
Copy Markdown

roylez commented Sep 19, 2010

"fg=none" is not a proper syntax. "none" means reset everything, including fg, bg, underline and etc. So you can have "none, fg=blue" to ensure blue text that is not in bold or underlined. --- Roy

@nicoulaj
Copy link
Copy Markdown
Author

Thank you, updated.

@dingram
Copy link
Copy Markdown

dingram commented Oct 4, 2010

Looks good, but fails under the following conditions in zsh 4.3.10:

  1. I have aliases for ls with various options: l, lr and lrt
  2. I then type lrtt and it highlights as I would expect, the whole way along (including lrtt as an error, as it does not exist)
  3. Hitting backspace to delete the last t still has the buffer highlighted as an error

@nicoulaj
Copy link
Copy Markdown
Author

nicoulaj commented Oct 4, 2010

This works fine for me... Maybe you have special keymapping config (zkbd ?). This is what the "backward-delete-char" is for.

@dingram
Copy link
Copy Markdown

dingram commented Oct 6, 2010

Aha! That made me think, and it turns out that I'm using vi-backward-delete-char. Adding the hook to it in the same way as lines 44 and 47 works beautifully :-)

@nicoulaj
Copy link
Copy Markdown
Author

Yep, I merged your changes.

@nicoulaj
Copy link
Copy Markdown
Author

I've moved this to a project:
Moved to: https://github.com/zsh-users/zsh-syntax-highlighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment