#
# Vi Mode
# This is an old script I have been updating/customizing over
# the time, I'm unable to mention the original author.
# Let me know if you know who I should give the credits to.
#
# This mode can be enabled/disabled using the variable
# $SHOWMODE in your $ZDOTLOCALDIR/zshrc.
#
# MiKael
#
if [[ $SHOWMODE != "on" ]]; then
# Vi showmode is not enabled--skipping file
return 0
fi
#####################
# Vim-like showmode #
#####################
# vi keybindings
bindkey -v
bindkey -M vicmd "^R" redo
bindkey -M vicmd "u" undo
bindkey -M vicmd "ga" what-cursor-position
unsetopt promptcr
setopt transient_rprompt
__string_insert="--INSERT--"
__string_normal="--NORMAL--"
__string_replace="--REPLACE-"
redisplay-insert() {
show_mode "$__string_insert"
}
redisplay-normal() {
show_mode "$__string_normal"
}
zle -N redisplay-insert
zle -N redisplay-normal
bindkey -M viins "^X^R" redisplay-insert
bindkey -M vicmd "^X^R" redisplay-normal
screenclear () {
echo -n "\033[2J\033[400H"
builtin zle .redisplay
show_mode "$__string_insert"
}
zle -N screenclear
bindkey "" screenclear
function zle-line-finish {
export RPS1="$__string_insert"
}
zle -N zle-line-finish
show_mode() {
export RPS1=$1
builtin zle reset-prompt
return
}
### vi-add-eol (unbound) (A) (unbound)
### Move to the end of the line and enter insert mode.
vi-add-eol() {
show_mode "$__string_insert"
builtin zle .vi-add-eol
}
zle -N vi-add-eol
bindkey -M vicmd "A" vi-add-eol
### vi-add-next (unbound) (a) (unbound)
### Enter insert mode after the current cursor posiĀ
### tion, without changing lines.
vi-add-next() {
show_mode "$__string_insert"
builtin zle .vi-add-next
}
zle -N vi-add-next
bindkey -M vicmd "a" vi-add-next
### vi-change (unbound) (c) (unbound)
### Read a movement command from the keyboard, and kill
### from the cursor position to the endpoint of the
### movement. Then enter insert mode. If the command
### is vi-change, change the current line.
vi-change() {
show_mode "$__string_insert"
builtin zle .vi-change
}
zle -N vi-change
bindkey -M vicmd "c" vi-change
### vi-change-eol (unbound) (C) (unbound)
### Kill to the end of the line and enter insert mode.
vi-change-eol() {
show_mode "$__string_insert"
builtin zle .vi-change-eol
}
zle -N vi-change-eol
bindkey -M vicmd "C" vi-change-eol
### vi-change-whole-line (unbound) (S) (unbound)
### Kill the current line and enter insert mode.
vi-change-whole-line() {
show_mode "$__string_insert"
builtin zle .vi-change-whole-line
}
zle -N vi-change-whole-line
bindkey -M vicmd "S" vi-change-whole-line
### vi-insert (unbound) (i) (unbound)
### Enter insert mode.
vi-insert() {
show_mode "$__string_insert"
builtin zle .vi-insert
}
zle -N vi-insert
bindkey -M vicmd "i" vi-insert
### vi-insert-bol (unbound) (I) (unbound)
### Move to the first non-blank character on the line
### and enter insert mode.
vi-insert-bol() {
show_mode "$__string_insert"
builtin zle .vi-insert-bol
}
zle -N vi-insert-bol
bindkey -M vicmd "I" vi-insert-bol
### vi-open-line-above (unbound) (O) (unbound)
### Open a line above the cursor and enter insert mode.
vi-open-line-above() {
show_mode "$__string_insert"
builtin zle .vi-open-line-above
}
zle -N vi-open-line-above
bindkey -M vicmd "O" vi-open-line-above
### vi-open-line-below (unbound) (o) (unbound)
### Open a line below the cursor and enter insert mode.
vi-open-line-below() {
show_mode "$__string_insert"
builtin zle .vi-open-line-below
}
zle -N vi-open-line-below
bindkey -M vicmd "o" vi-open-line-below
### vi-substitute (unbound) (s) (unbound)
### Substitute the next character(s).
vi-substitute() {
show_mode "$__string_insert"
builtin zle .vi-substitute
}
zle -N vi-substitute
bindkey -M vicmd "s" vi-substitute
### vi-replace (unbound) (R) (unbound)
### Enter overwrite mode.
###
vi-replace() {
show_mode "$__string_replace"
builtin zle .vi-replace
}
zle -N vi-replace
bindkey -M vicmd "R" vi-replace
### vi-cmd-mode (^X^V) (unbound) (^[)
### Enter command mode; that is, select the `vicmd'
### keymap. Yes, this is bound by default in emacs
### mode.
vi-cmd-mode() {
show_mode "$__string_normal"
builtin zle .vi-cmd-mode
}
zle -N vi-cmd-mode
bindkey -M viins "" vi-cmd-mode
### vi-oper-swap-case
### Read a movement command from the keyboard, and swap
### the case of all characters from the cursor position
### to the endpoint of the movement. If the movement
### command is vi-oper-swap-case, swap the case of all
### characters on the current line.
###
bindkey -M vicmd "g~" vi-oper-swap-case
bindkey -M vicmd "[2~" vi-insert
bindkey -M vicmd 'q' push-line
bindkey -M vicmd '[5~' history-beginning-search-backward # PgUp
bindkey -M vicmd '[6~' history-beginning-search-forward # PgDn
bindkey -M viins '[5~' history-beginning-search-backward # PgUp
bindkey -M viins '[6~' history-beginning-search-forward # PgDn
bindkey -M viins '[7~' beginning-of-line
bindkey -M viins '[8~' end-of-line
bindkey -M viins '^A' beginning-of-line
bindkey -M viins '^E' end-of-line
bindkey -M viins '[3~' delete-char
bindkey -M viins '[A' up-line-or-history
bindkey -M viins '[B' down-line-or-history
bindkey -M viins '^Xc' copy-prev-word
bindkey -M viins '^Xf' _correct_filename
bindkey -M viins '^_' undo
bindkey -M viins '^K' vi-change-eol
bindkey -M viins '^R' history-incremental-search-backward
bindkey -M viins '^O' accept-line-and-down-history
bindkey -M viins '^T' transpose-chars
bindkey -M viins '^Xm' _most_recent_file
bindkey -M viins '^P' _history-complete-older
bindkey -M viins '^N' _history-complete-newer
bindkey -M viins -s '^X^H' 'hash -r\n'
# These bindings depend on the ftzle script
bindkey -M viins "^X^W" backward-kill-to-slash
bindkey '^I' ft-complete
if [[ -z ${TERM:#screen} ]]; then
bindkey -M viins '[1~' beginning-of-line # Home
bindkey -M viins '[4~' end-of-line # End
fi
# Edit the command line using your usual editor.
autoload edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line
export RPS1="$__string_insert"