zshrc.d/60-vi-showmode.zrc
author Mikael Berthe <mikael@lilotux.net>
Sat, 14 Jul 2012 19:20:57 +0200
changeset 0 7215ca490221
child 8 3f68f355bf1a
permissions -rw-r--r--
Re-import repository after cleanup from personal stuff (see description) I've remove too dirty or specific stuff, so that i could put this configuration on a public repo. Here's a copy of the initial Mercurial changelog, FWIW... * (2012-07-14) Mikael Berthe Reorganize all scripts * (2012-07-14) Mikael Berthe Add a "tools" directory * (2012-07-14) Mikael Berthe Don't use ^X^C as a keybinding * (2012-07-14) Mikael Berthe Disable ... completion for now * (2012-07-14) Mikael Berthe Add rehash key binding * (2012-07-14) Mikael Berthe Add n fix a few key bindings * (2012-07-14) Mikael Berthe More cleanup Remove zshrc_compinstall Move personnal stuff to zshrc_local * (2012-07-14) Mikael Berthe New commands: ...* Previous commands .. and ... have been renamed to ,, and ,,, * (2012-07-14) Mikael Berthe Add WATCHFMT, move REPORTTIME to zshenv * (2012-07-14) Mikael Berthe Add REPORTTIME * (2012-07-13) Mikael Berthe Add a few useful functions Ideas borrowed from https://github.com/whiteinge/dotfiles/blob/master/.zshrc * (2012-07-13) Mikael Berthe Move aliases to a new file * (2012-07-13) Mikael Berthe Check for zshrc_local existence * (2012-07-13) Mikael Berthe Cleanup vcsinfo prompt * (2012-07-12) Mikael Berthe Colorize time in default color prompt as well * (2012-07-12) Mikael Berthe Prompt: colorize time * (2012-07-12) Mikael Berthe Add a copy of git-prompt.sh * (2012-07-12) Mikael Berthe Update prompt themes Since we do not display the mode on the first line, let's use this place for the date/time. * (2012-07-12) Mikael Berthe Update prompts / new showmode * (2012-07-12) Mikael Berthe More showmode cleanup, part2 * (2012-07-12) Mikael Berthe Switch showmode to using RPROMPT, part1 * (2012-07-12) Mikael Berthe Prompt: many improvements and fixes * (2012-07-11) Mikael Berthe More prompt code cleanup * (2012-07-11) Mikael Berthe More readable prompt generation * (2012-07-11) Mikael Berthe Dynamic path color * (2012-07-11) Mikael Berthe New prompt based on Zsh' vcs_info module * (2012-07-10) Mikael Berthe Add git info, add a light dev2 prompt * (2012-07-10) Mikael Berthe Dev prompt: Improve Mercurial repository detection * (2012-07-08) Mikael Berthe hg prompt: Change color of unapplied patches * (2012-07-08) Mikael Berthe Change Mercurial prompt format * (2012-07-08) Mikael Berthe Try to improve showmode support. Enable it for 256 color terms. * (2012-07-08) Mikael Berthe Fix interaction between showmode and the new "dev" prompt When showmode is disabled (e.g. in screen), do not display the mode in PS1. * (2012-07-08) Mikael Berthe Add development prompt (HG and minimal git support) Can be set up from the shell command line with "prompt_set_dev". * (2012-07-08) Mikael Berthe Ignore Vim swap files * (2009-12-22) Mikael Berthe Slight update zlog{in,out} * (2009-12-22) Mikael Berthe Add zlogin * (2009-12-22) Mikael Berthe Update hgignore list * (2009-12-22) Mikael Berthe Initial import into Mercurial

#
# 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 ''  up-line-or-history
bindkey -M viins ''  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 -s '^X^H' 'hash -r\n'


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"