├── README.md ├── extra └── zsh-syntax-hl-ft.png └── zsh-syntax-highlighting-filetypes.zsh /README.md: -------------------------------------------------------------------------------- 1 | [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=65SFZJ25PSKG8¤cy_code=SEK&source=url) - Every tiny cent helps a lot! 2 | 3 | zsh-syntax-highlighting-filetypes 4 | ================================= 5 | 6 | ![image](/extra/zsh-syntax-hl-ft.png) 7 | 8 | LS\_COLORS on your commandline in realtime as you type. 9 | 10 | This is based on nicoulaj's [zsh-syntax-highlighting][0] project. I've taken the 11 | initial version of his script and added highlighting for filetypes and a few 12 | other things. The filetype highlighting rules are taken from my [LS_COLORS][1] project. 13 | 14 | This will only work in terminals capable of displaying 256 colors. 15 | 16 | [0]: https://github.com/zsh-users/zsh-syntax-highlighting 17 | [1]: https://github.com/trapd00r/LS_COLORS 18 | -------------------------------------------------------------------------------- /extra/zsh-syntax-hl-ft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trapd00r/zsh-syntax-highlighting-filetypes/69b77b1105a12c1efaddfb2f46faa306743d18bb/extra/zsh-syntax-hl-ft.png -------------------------------------------------------------------------------- /zsh-syntax-highlighting-filetypes.zsh: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | # Name: zsh-syntax-highlighting-filetypes 3 | # Author: Magnus Woldrich 4 | # Update: 2011-06-20 23:26:38 5 | # 6 | # This is based on nicoulaj's zsh-syntax-highlighting project [0]. I've 7 | # taken the initial version of his script and added highlighting for 8 | # filetypes and a few other things. The filetype highlighting rules are 9 | # taken from my LS_COLORS [1] project. 10 | # 11 | # This will only work in terminals capable of displaying 256 colors. 12 | # 13 | # [0]: https://github.com/nicoulaj/zsh-syntax-highlighting 14 | # [1]: https://github.com/trapd00r/LS_COLORS 15 | 16 | 17 | # Core highlighting update system 18 | 19 | # Array used by highlighters to declare overridable styles. 20 | typeset -gA ZSH_HIGHLIGHT_STYLES 21 | 22 | # An `object' implemented by below 3 arrays' elements could be called a 23 | # `highlighter', registered by `_zsh_highlight_add-highlighter`. In 24 | # other words, these arrays are indexed and tied by their own 25 | # functionality. If they have been arranged inconsistently, things goes 26 | # wrong. Please see `_zsh_highlight-zle-buffer` and `_zsh_highlight_add- 27 | # highlighter`. 28 | 29 | 30 | # Actual recolorize functions to be called. 31 | typeset -a zsh_highlight_functions; zsh_highlight_functions=() 32 | 33 | # Predicate functions whether its recolorize function should be called or not. 34 | typeset -a zsh_highlight_predicates; zsh_highlight_predicates=() 35 | 36 | # Highlight storages for each recolorize functions. 37 | typeset -a zsh_highlight_caches; zsh_highlight_caches=() 38 | 39 | _zsh_highlight-zle-buffer() { 40 | if (( PENDING )); then 41 | return 42 | fi 43 | 44 | local ret=$? 45 | { 46 | local -a funinds 47 | local -i rh_size=$#region_highlight 48 | for i in {1..${#zsh_highlight_functions}}; do 49 | local pred=${zsh_highlight_predicates[i]} 50 | local cache_place=${zsh_highlight_caches[i]} 51 | if _zsh_highlight-zle-buffer-p "$rh_size" "$pred"; then 52 | if ((${#${(P)cache_place}} > 0)); then 53 | region_highlight=(${region_highlight:#(${(P~j.|.)cache_place})}) 54 | local -a empty; empty=(); : ${(PA)cache_place::=$empty} 55 | fi 56 | funinds+=$i 57 | fi 58 | done 59 | for i in $funinds; do 60 | local func=${zsh_highlight_functions[i]} 61 | local cache_place=${zsh_highlight_caches[i]} 62 | local -a rh; rh=($region_highlight) 63 | { 64 | "$func" 65 | } always { 66 | : ${(PA)cache_place::=${region_highlight:#(${(~j.|.)rh})}} 67 | } 68 | done 69 | } always { 70 | ZSH_PRIOR_CURSOR=$CURSOR 71 | ZSH_PRIOR_HIGHLIGHTED_BUFFER=$BUFFER 72 | return $ret 73 | } 74 | } 75 | 76 | # Whether supplied highlight_predicate satisfies or not. 77 | _zsh_highlight-zle-buffer-p() { 78 | local region_highlight_size="$1" highlight_predicate="$2" 79 | # If any highlightings are not taken into account, asume it is needed. 80 | # This holds for some up/down-history commands, for example. 81 | ((region_highlight_size == 0)) || "$highlight_predicate" 82 | } 83 | 84 | # Whether the command line buffer is modified or not. 85 | _zsh_highlight_buffer-modified-p() { 86 | [[ ${ZSH_PRIOR_HIGHLIGHTED_BUFFER:-} != $BUFFER ]] 87 | } 88 | 89 | # Whether the cursor is moved or not. 90 | _zsh_highlight_cursor-moved-p() { 91 | ((ZSH_PRIOR_CURSOR != $CURSOR)) 92 | } 93 | 94 | # Register an highlighter. 95 | _zsh_highlight_add-highlighter() { 96 | zsh_highlight_functions+="$1" 97 | zsh_highlight_predicates+="${2-${1}-p}" 98 | zsh_highlight_caches+="${3-${1//-/_}}" 99 | } 100 | 101 | 102 | # Main highlighter 103 | 104 | ZSH_HIGHLIGHT_STYLES+=( 105 | default 'fg=248' 106 | unknown-token 'fg=196,bold,bg=234' 107 | reserved-word 'fg=197,bold' 108 | alias 'fg=197,bold' 109 | builtin 'fg=107,bold' 110 | function 'fg=85,bold' 111 | command 'fg=166,bold' 112 | hashed-command 'fg=70' 113 | path 'fg=30' 114 | globbing 'fg=170,bold' 115 | history-expansion 'fg=blue' 116 | single-hyphen-option 'fg=244' 117 | double-hyphen-option 'fg=244' 118 | back-quoted-argument 'fg=220,bold' 119 | single-quoted-argument 'fg=137' 120 | double-quoted-argument 'fg=137' 121 | dollar-double-quoted-argument 'fg=148' 122 | back-double-quoted-argument 'fg=172,bold' 123 | assign 'fg=240,bold' 124 | ) 125 | 126 | mkstyle () { 127 | local lastlast 128 | local last 129 | 130 | while [ "$#" -gt 0 ]; do 131 | cur=$1 132 | shift 133 | 134 | if [ "$last" = 5 ]; then 135 | if [ "$lastlast" = 38 ]; then 136 | style+=( "fg=$cur" ) 137 | lastlast= 138 | last= 139 | continue 140 | elif [ "$lastlast" = 48 ]; then 141 | style+=( "bg=$cur" ) 142 | lastlast= 143 | last= 144 | continue 145 | fi 146 | fi 147 | 148 | lastlast=$last 149 | last=$cur 150 | done 151 | 152 | case "$last" in 153 | 00|0) style+=( "none" ) ;; 154 | 01|1) style+=( "bold" ) ;; 155 | 04|4) style+=( "underscore" ) ;; 156 | 05|5) style+=( "blink" ) ;; 157 | 07|7) style+=( "reverse" ) ;; 158 | 08|8) style+=( "concealed" ) ;; 159 | esac 160 | } 161 | 162 | function { 163 | local coloring ext rawstyle 164 | local -a style 165 | 166 | for coloring in ${(s.:.)LS_COLORS}; do 167 | ext=${coloring%%\=*} 168 | rawstyle=${coloring##*\=} 169 | style=() 170 | 171 | mkstyle ${(s.;.)rawstyle} 172 | style=${(j.,.)style} 173 | 174 | ZSH_HIGHLIGHT_STYLES+=( 175 | "$ext" "$style" 176 | ) 177 | done 178 | } 179 | 180 | # Tokens that are always immediately followed by a command. 181 | ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS=( 182 | '|' '||' ';' '&' '&&' 'noglob' 'nocorrect' 'builtin' 183 | ) 184 | 185 | # Check if the argument is variable assignment 186 | _zsh_highlight_check-assign() { 187 | setopt localoptions extended_glob 188 | [[ ${(Q)arg} == [[:alpha:]_]([[:alnum:]_])#=* ]] 189 | } 190 | 191 | # Check if the argument is a path. 192 | _zsh_highlight_check-path() { 193 | [[ -z ${(Q)arg} ]] && return 1 194 | [[ -e ${(Q)arg} ]] && return 0 195 | [[ ! -e ${(Q)arg:h} ]] && return 1 196 | [[ ${#BUFFER} == $end_pos && -n $(print ${(Q)arg}*(N)) ]] && return 0 197 | return 1 198 | } 199 | 200 | # Highlight special chars inside double-quoted strings 201 | _zsh_highlight_highlight_string() { 202 | setopt localoptions noksharrays 203 | local i j k style 204 | # Starting quote is at 1, so start parsing at offset 2 in the string. 205 | for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do 206 | (( j = i + start_pos - 1 )) 207 | (( k = j + 1 )) 208 | case "$arg[$i]" in 209 | '$') style=$ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument];; 210 | '%') style=$ZSH_HIGHLIGHT_STYLES[globbing];; 211 | '^') style=$ZSH_HIGHLIGHT_STYLES[globbing];; 212 | "\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument] 213 | (( k += 1 )) # Color following char too. 214 | (( i += 1 )) # Skip parsing the escaped char. 215 | ;; 216 | *) continue;; 217 | esac 218 | region_highlight+=("$j $k $style") 219 | done 220 | } 221 | 222 | # Core syntax highlighting. 223 | _zsh_main-highlight() { 224 | setopt localoptions extendedglob bareglobqual 225 | local start_pos=0 end_pos highlight_glob=true new_expression=true arg style 226 | region_highlight=() 227 | 228 | for arg in ${(z)BUFFER}; do 229 | local substr_color=0 230 | 231 | style= 232 | 233 | [[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false 234 | 235 | ((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]##[[:space:]]#}})) 236 | ((end_pos=$start_pos+${#arg})) 237 | 238 | if $new_expression; then 239 | new_expression=false 240 | 241 | res=$(LC_ALL=C builtin type -w $arg 2>/dev/null) 242 | case $res in 243 | *': reserved') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];; 244 | *': alias') style=$ZSH_HIGHLIGHT_STYLES[alias] 245 | local aliased_command="${"$(alias $arg)"#*=}" 246 | [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$aliased_command"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS+=($arg) 247 | ;; 248 | *': builtin') style=$ZSH_HIGHLIGHT_STYLES[builtin];; 249 | *': function') style=$ZSH_HIGHLIGHT_STYLES[function];; 250 | *': command') style=$ZSH_HIGHLIGHT_STYLES[command];; 251 | *': hashed') style=$ZSH_HIGHLIGHT_STYLES[hashed-command];; 252 | *) if _zsh_highlight_check-assign; then 253 | style=$ZSH_HIGHLIGHT_STYLES[assign] 254 | new_expression=true 255 | elif _zsh_highlight_check-path; then 256 | style=$ZSH_HIGHLIGHT_STYLES[path] 257 | elif [[ $arg[0,1] = $histchars[0,1] ]]; then 258 | style=$ZSH_HIGHLIGHT_STYLES[history-expansion] 259 | else 260 | style=$ZSH_HIGHLIGHT_STYLES[unknown-token] 261 | fi 262 | ;; 263 | esac 264 | else 265 | for key in ${(k)ZSH_HIGHLIGHT_STYLES}; do 266 | case $key in 267 | "*."*) ;; 268 | *) continue ;; 269 | esac 270 | case $arg in 271 | *.$key[3,-1]) style=$ZSH_HIGHLIGHT_STYLES[$key] ;; 272 | esac 273 | [ -n "$style" ] && break 274 | done 275 | if [ -z "$style" ]; then 276 | case $arg in 277 | '--'*) style=$ZSH_HIGHLIGHT_STYLES[double-hyphen-option];; 278 | '-'*) style=$ZSH_HIGHLIGHT_STYLES[single-hyphen-option];; 279 | "'"*"'") style=$ZSH_HIGHLIGHT_STYLES[single-quoted-argument];; 280 | '"'*'"') style=$ZSH_HIGHLIGHT_STYLES[double-quoted-argument] 281 | region_highlight+=("$start_pos $end_pos $style") 282 | _zsh_highlight_highlight_string 283 | substr_color=1 284 | ;; 285 | '`'*'`') style=$ZSH_HIGHLIGHT_STYLES[back-quoted-argument];; 286 | *"*"*) $highlight_glob && style=$ZSH_HIGHLIGHT_STYLES[globbing] || 287 | style=$ZSH_HIGHLIGHT_STYLES[default];; 288 | *) if _zsh_highlight_check-path; then 289 | style=$ZSH_HIGHLIGHT_STYLES[path] 290 | elif [[ $arg[0,1] = $histchars[0,1] ]]; then 291 | style=$ZSH_HIGHLIGHT_STYLES[history-expansion] 292 | else 293 | style=$ZSH_HIGHLIGHT_STYLES[default] 294 | fi 295 | ;; 296 | esac 297 | fi 298 | fi 299 | [[ $substr_color = 0 ]] && 300 | region_highlight+=("$start_pos $end_pos $style") 301 | [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true 302 | start_pos=$end_pos 303 | done 304 | } 305 | 306 | 307 | # Setup functions 308 | 309 | # Intercept specified ZLE events to have highlighting triggered. 310 | _zsh_highlight_bind-events() { 311 | 312 | # Resolve event names what have to be bound to. 313 | zmodload zsh/zleparameter 2>/dev/null || { 314 | echo 'zsh-syntax-highlighting:zmodload error. exiting.' >&2 315 | return -1 316 | } 317 | local -a events; : ${(A)events::=${@:#(_*|orig-*|.run-help|.which-command)}} 318 | 319 | # Bind the events to _zsh_highlight-zle-buffer. 320 | local clean_event 321 | for event in $events; do 322 | if [[ "$widgets[$event]" == completion:* ]]; then 323 | eval "zle -C orig-$event ${${${widgets[$event]}#*:}/:/ } ; $event() { builtin zle orig-$event && _zsh_highlight-zle-buffer } ; zle -N $event" 324 | else 325 | case $event in 326 | accept-and-menu-complete) 327 | eval "$event() { builtin zle .$event && _zsh_highlight-zle-buffer } ; zle -N $event" 328 | ;; 329 | .*) 330 | # Remove the leading dot in the event name 331 | clean_event=$event[2,${#event}] 332 | case ${widgets[$clean_event]-} in 333 | (completion|user):*) 334 | ;; 335 | *) 336 | eval "$clean_event() { builtin zle $event && _zsh_highlight-zle-buffer } ; zle -N $clean_event" 337 | ;; 338 | esac 339 | ;; 340 | *) 341 | ;; 342 | esac 343 | fi 344 | done 345 | } 346 | 347 | # Load highlighters from specified directory if it exists. 348 | _zsh_highlight_load-highlighters() { 349 | [[ -d $1 ]] && for highlighter_def ($1/*.zsh) . $highlighter_def 350 | } 351 | 352 | 353 | # Setup 354 | 355 | # Bind highlighting to all known events. 356 | _zsh_highlight_bind-events "${(@f)"$(zle -la)"}" 357 | 358 | # Register the main highlighter. 359 | _zsh_highlight_add-highlighter _zsh_main-highlight _zsh_highlight_buffer-modified-p 360 | 361 | # Load additional highlighters if available. 362 | _zsh_highlight_load-highlighters "${${(%):-%N}:h}/highlighters" 363 | --------------------------------------------------------------------------------