# Exposes information about the Zsh Line Editor via the $editor_info associative # array. function editor-info { # Clean up previous $editor_info. unset editor_info typeset -gA editor_info if [[ "$KEYMAP" == 'vicmd' ]]; then zstyle -s ':editor:info:keymap:alternate' format 'REPLY' editor_info[keymap]="$REPLY" else zstyle -s ':editor:info:keymap:primary' format 'REPLY' editor_info[keymap]="$REPLY" if [[ "$ZLE_STATE" == *overwrite* ]]; then zstyle -s ':editor:info:keymap:primary:overwrite' format 'REPLY' editor_info[overwrite]="$REPLY" else zstyle -s ':editor:info:keymap:primary:insert' format 'REPLY' editor_info[overwrite]="$REPLY" fi fi unset REPLY zle reset-prompt zle -R } zle -N editor-info # Updates editor information when the keymap changes. function zle-keymap-select { zle editor-info } zle -N zle-keymap-select # Enables terminal application mode and updates editor information. function zle-line-init { # The terminal must be in application mode when ZLE is active for $terminfo # values to be valid. if (( $+terminfo[smkx] )); then # Enable terminal application mode. echoti smkx fi # Update editor information. zle editor-info } zle -N zle-line-init # Disables terminal application mode and updates editor information. function zle-line-finish { # The terminal must be in application mode when ZLE is active for $terminfo # values to be valid. if (( $+terminfo[rmkx] )); then # Disable terminal application mode. echoti rmkx fi # Update editor information. zle editor-info } zle -N zle-line-finish # Toggles emacs overwrite mode and updates editor information. function overwrite-mode { zle .overwrite-mode zle editor-info } zle -N overwrite-mode # Enters vi insert mode and updates editor information. function vi-insert { zle .vi-insert zle editor-info } zle -N vi-insert # Moves to the first non-blank character then enters vi insert mode and updates # editor information. function vi-insert-bol { zle .vi-insert-bol zle editor-info } zle -N vi-insert-bol # Enters vi replace mode and updates editor information. function vi-replace { zle .vi-replace zle editor-info } zle -N vi-replace # Expands .... to ../.. function expand-dot-to-parent-directory-path { if [[ $LBUFFER = *.. ]]; then LBUFFER+='/..' else LBUFFER+='.' fi } zle -N expand-dot-to-parent-directory-path # Displays an indicator when completing. function expand-or-complete-with-indicator { local indicator zstyle -s ':editor:info:completing' format 'indicator' print -Pn "$indicator" zle expand-or-complete zle redisplay } zle -N expand-or-complete-with-indicator # Inserts 'sudo ' at the beginning of the line. function prepend-sudo { if [[ "$BUFFER" != su(do|)\ * ]]; then BUFFER="sudo $BUFFER" (( CURSOR += 5 )) fi } zle -N prepend-sudo # Reset to default key bindings. bindkey -d