You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
237 lines
6.3 KiB
237 lines
6.3 KiB
13 years ago
|
#
|
||
|
# Sets keyboard bindings.
|
||
|
#
|
||
|
# Authors:
|
||
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
||
|
#
|
||
|
|
||
13 years ago
|
# Dumb terminals lack support.
|
||
|
if [[ "$TERM" == 'dumb' ]]; then
|
||
13 years ago
|
return 1
|
||
13 years ago
|
fi
|
||
|
|
||
13 years ago
|
# The default styles.
|
||
13 years ago
|
|
||
13 years ago
|
# Indicator to notify of vi insert mode.
|
||
|
zstyle ':omz:prompt:vi' insert '>>>'
|
||
|
|
||
13 years ago
|
# Indicator to notify of vi command mode.
|
||
13 years ago
|
zstyle ':omz:prompt:vi' command '<<<'
|
||
13 years ago
|
|
||
|
# Indicator to notify of generating completion.
|
||
13 years ago
|
zstyle ':omz:editor' completing '...'
|
||
13 years ago
|
|
||
14 years ago
|
# Beep on error in line editor.
|
||
13 years ago
|
setopt BEEP
|
||
15 years ago
|
|
||
14 years ago
|
# Allow command line editing in an external editor.
|
||
|
autoload -Uz edit-command-line
|
||
|
zle -N edit-command-line
|
||
|
|
||
14 years ago
|
# Use human-friendly identifiers.
|
||
13 years ago
|
zmodload zsh/terminfo
|
||
13 years ago
|
typeset -gA keyinfo
|
||
13 years ago
|
keyinfo=(
|
||
14 years ago
|
'Control' '\C-'
|
||
|
'Escape' '\e'
|
||
|
'Meta' '\M-'
|
||
13 years ago
|
'Backspace' "^?"
|
||
|
'Delete' "^[[3~"
|
||
13 years ago
|
'F1' "$terminfo[kf1]"
|
||
|
'F2' "$terminfo[kf2]"
|
||
|
'F3' "$terminfo[kf3]"
|
||
|
'F4' "$terminfo[kf4]"
|
||
|
'F5' "$terminfo[kf5]"
|
||
|
'F6' "$terminfo[kf6]"
|
||
|
'F7' "$terminfo[kf7]"
|
||
|
'F8' "$terminfo[kf8]"
|
||
|
'F9' "$terminfo[kf9]"
|
||
|
'F10' "$terminfo[kf10]"
|
||
|
'F11' "$terminfo[kf11]"
|
||
|
'F12' "$terminfo[kf12]"
|
||
|
'Insert' "$terminfo[kich1]"
|
||
|
'Home' "$terminfo[khome]"
|
||
|
'PageUp' "$terminfo[kpp]"
|
||
|
'End' "$terminfo[kend]"
|
||
|
'PageDown' "$terminfo[knp]"
|
||
|
'Up' "$terminfo[kcuu1]"
|
||
|
'Left' "$terminfo[kcub1]"
|
||
|
'Down' "$terminfo[kcud1]"
|
||
|
'Right' "$terminfo[kcuf1]"
|
||
|
'BackTab' "$terminfo[kcbt]"
|
||
14 years ago
|
)
|
||
15 years ago
|
|
||
13 years ago
|
# Do not bind any keys if there are empty values in $keyinfo.
|
||
|
for key in "$keyinfo[@]"; do
|
||
|
if [[ -z "$key" ]]; then
|
||
|
print "omz: one or more keys are non-bindable" >&2
|
||
13 years ago
|
return 1
|
||
13 years ago
|
fi
|
||
|
done
|
||
|
|
||
13 years ago
|
# Displays the current vi mode.
|
||
|
function zle-line-init zle-line-finish zle-keymap-select {
|
||
|
if [[ "$KEYMAP" == 'vicmd' ]]; then
|
||
|
zstyle -s ':omz:prompt:vi' command 'vi_prompt_info'
|
||
|
else
|
||
|
zstyle -s ':omz:prompt:vi' insert 'vi_prompt_info'
|
||
13 years ago
|
fi
|
||
13 years ago
|
zle reset-prompt
|
||
|
zle -R
|
||
13 years ago
|
}
|
||
|
zle -N zle-line-init
|
||
|
zle -N zle-line-finish
|
||
13 years ago
|
zle -N zle-keymap-select
|
||
13 years ago
|
|
||
13 years ago
|
# Expands .... to ../..
|
||
13 years ago
|
function expand-dot-to-parent-directory-path {
|
||
13 years ago
|
if [[ $LBUFFER = *.. ]]; then
|
||
|
LBUFFER+='/..'
|
||
|
else
|
||
|
LBUFFER+='.'
|
||
|
fi
|
||
|
}
|
||
|
zle -N expand-dot-to-parent-directory-path
|
||
|
|
||
|
# Displays an indicator when completing.
|
||
13 years ago
|
function expand-or-complete-with-indicator {
|
||
13 years ago
|
local indicator
|
||
13 years ago
|
zstyle -s ':omz:editor' completing 'indicator'
|
||
13 years ago
|
print -Pn "$indicator"
|
||
|
zle expand-or-complete-prefix
|
||
|
zle redisplay
|
||
|
}
|
||
13 years ago
|
zle -N expand-or-complete-with-indicator
|
||
13 years ago
|
|
||
13 years ago
|
# Inserts 'sudo ' at the beginning of the line.
|
||
13 years ago
|
function prepend-sudo {
|
||
13 years ago
|
if [[ "$BUFFER" != su(do|)\ * ]]; then
|
||
|
BUFFER="sudo $BUFFER"
|
||
|
(( CURSOR += 5 ))
|
||
|
fi
|
||
|
}
|
||
|
zle -N prepend-sudo
|
||
|
|
||
13 years ago
|
# Reset to default key bindings.
|
||
|
bindkey -d
|
||
|
|
||
13 years ago
|
# Emacs key bindings.
|
||
13 years ago
|
for key in "$keyinfo[Escape]"{B,b}; \
|
||
|
bindkey -M emacs "$key" emacs-backward-word
|
||
|
for key in "$keyinfo[Escape]"{F,f}; \
|
||
|
bindkey -M emacs "$key" emacs-forward-word
|
||
|
bindkey -M emacs "$keyinfo[Escape]$keyinfo[Left]" emacs-backward-word
|
||
|
bindkey -M emacs "$keyinfo[Escape]$keyinfo[Right]" emacs-forward-word
|
||
13 years ago
|
|
||
|
# Kill to the beginning of the line.
|
||
13 years ago
|
for key in "$keyinfo[Escape]"{K,k}; \
|
||
|
bindkey -M emacs "$key" backward-kill-line
|
||
14 years ago
|
|
||
13 years ago
|
# Redo.
|
||
13 years ago
|
bindkey -M emacs "$keyinfo[Escape]_" redo
|
||
14 years ago
|
|
||
13 years ago
|
# Search previous character.
|
||
13 years ago
|
bindkey -M emacs "$keyinfo[Control]X$keyinfo[Control]B" vi-find-prev-char
|
||
13 years ago
|
|
||
13 years ago
|
# Match bracket.
|
||
13 years ago
|
bindkey -M emacs "$keyinfo[Control]X$keyinfo[Control]]" vi-match-bracket
|
||
14 years ago
|
|
||
13 years ago
|
# Edit command in an external editor.
|
||
13 years ago
|
bindkey -M emacs "$keyinfo[Control]X$keyinfo[Control]E" edit-command-line
|
||
14 years ago
|
|
||
13 years ago
|
if (( $+widgets[history-incremental-pattern-search-backward] )); then
|
||
13 years ago
|
bindkey -M emacs "$keyinfo[Control]R" \
|
||
|
history-incremental-pattern-search-backward
|
||
|
bindkey -M emacs "$keyinfo[Control]S" \
|
||
|
history-incremental-pattern-search-forward
|
||
13 years ago
|
fi
|
||
14 years ago
|
|
||
13 years ago
|
# Vi key bindings.
|
||
14 years ago
|
|
||
13 years ago
|
# Edit command in an external editor.
|
||
|
bindkey -M vicmd "v" edit-command-line
|
||
14 years ago
|
|
||
13 years ago
|
# Undo/Redo
|
||
|
bindkey -M vicmd "u" undo
|
||
13 years ago
|
bindkey -M vicmd "$keyinfo[Control]R" redo
|
||
14 years ago
|
|
||
13 years ago
|
# Switch to command mode.
|
||
|
bindkey -M viins "jk" vi-cmd-mode
|
||
|
bindkey -M viins "kj" vi-cmd-mode
|
||
14 years ago
|
|
||
13 years ago
|
if (( $+widgets[history-incremental-pattern-search-backward] )); then
|
||
|
bindkey -M vicmd "?" history-incremental-pattern-search-backward
|
||
|
bindkey -M vicmd "/" history-incremental-pattern-search-forward
|
||
|
else
|
||
|
bindkey -M vicmd "?" history-incremental-search-backward
|
||
|
bindkey -M vicmd "/" history-incremental-search-forward
|
||
|
fi
|
||
13 years ago
|
|
||
13 years ago
|
# Emacs and Vi key bindings.
|
||
|
for keymap in 'emacs' 'viins'; do
|
||
13 years ago
|
bindkey -M "$keymap" "$keyinfo[Home]" beginning-of-line
|
||
|
bindkey -M "$keymap" "$keyinfo[End]" end-of-line
|
||
|
|
||
|
bindkey -M "$keymap" "$keyinfo[Insert]" overwrite-mode
|
||
|
bindkey -M "$keymap" "$keyinfo[Delete]" delete-char
|
||
|
bindkey -M "$keymap" "$keyinfo[Backspace]" backward-delete-char
|
||
|
|
||
|
bindkey -M "$keymap" "$keyinfo[Left]" backward-char
|
||
|
bindkey -M "$keymap" "$keyinfo[Right]" forward-char
|
||
13 years ago
|
|
||
|
# Expand history on space.
|
||
|
bindkey -M "$keymap" ' ' magic-space
|
||
14 years ago
|
|
||
13 years ago
|
# Clear screen.
|
||
13 years ago
|
bindkey -M "$keymap" "$keyinfo[Control]L" clear-screen
|
||
14 years ago
|
|
||
13 years ago
|
# Expand command name to full path.
|
||
13 years ago
|
for key in "$keyinfo[Escape]"{E,e}; \
|
||
|
bindkey -M "$keymap" "$key" expand-cmd-path
|
||
14 years ago
|
|
||
13 years ago
|
# Duplicate the previous word.
|
||
13 years ago
|
for key in "$keyinfo[Escape]"{M,m}; \
|
||
|
bindkey -M "$keymap" "$key" copy-prev-shell-word
|
||
14 years ago
|
|
||
13 years ago
|
# Use a more flexible push-line.
|
||
13 years ago
|
for key in "$keyinfo[Control]Q" "$keyinfo[Escape]"{q,Q}; \
|
||
|
bindkey -M "$keymap" "$key" push-line-or-edit
|
||
14 years ago
|
|
||
13 years ago
|
# Bind Shift + Tab to go to the previous menu item.
|
||
13 years ago
|
bindkey -M "$keymap" "$keyinfo[BackTab]" reverse-menu-complete
|
||
14 years ago
|
|
||
13 years ago
|
# Complete in the middle of word.
|
||
13 years ago
|
bindkey -M "$keymap" "$keyinfo[Control]I" expand-or-complete-prefix
|
||
14 years ago
|
|
||
13 years ago
|
# Expand .... to ../..
|
||
13 years ago
|
if zstyle -t ':omz:editor' dot-expansion; then
|
||
13 years ago
|
bindkey -M "$keymap" "." expand-dot-to-parent-directory-path
|
||
13 years ago
|
fi
|
||
14 years ago
|
|
||
13 years ago
|
# Display an indicator when completing.
|
||
13 years ago
|
bindkey -M "$keymap" "$keyinfo[Control]I" \
|
||
|
expand-or-complete-with-indicator
|
||
14 years ago
|
|
||
13 years ago
|
# Insert 'sudo ' at the beginning of the line.
|
||
13 years ago
|
bindkey -M "$keymap" "$keyinfo[Control]X$keyinfo[Control]S" prepend-sudo
|
||
13 years ago
|
done
|
||
13 years ago
|
|
||
13 years ago
|
# Do not expand .... to ../.. during incremental search.
|
||
|
if zstyle -t ':omz:editor' dot-expansion; then
|
||
|
bindkey -M isearch . self-insert 2> /dev/null
|
||
|
fi
|
||
|
|
||
13 years ago
|
# Set the key layout.
|
||
|
zstyle -s ':omz:editor' keymap 'keymap'
|
||
|
if [[ "$keymap" == (emacs|) ]]; then
|
||
|
bindkey -e
|
||
|
elif [[ "$keymap" == vi ]]; then
|
||
|
bindkey -v
|
||
|
else
|
||
|
print "omz: invalid keymap: $keymap" >&2
|
||
|
fi
|
||
13 years ago
|
|
||
13 years ago
|
unset keymap
|
||
13 years ago
|
unset key
|
||
|
|