zshrc.d/60-vi-showmode.zrc
changeset 0 7215ca490221
child 8 3f68f355bf1a
equal deleted inserted replaced
-1:000000000000 0:7215ca490221
       
     1 #
       
     2 # Vi Mode
       
     3 # This is an old script I have been updating/customizing over
       
     4 # the time, I'm unable to mention the original author.
       
     5 # Let me know if you know who I should give the credits to.
       
     6 #
       
     7 # This mode can be enabled/disabled using the variable
       
     8 # $SHOWMODE in your $ZDOTLOCALDIR/zshrc.
       
     9 #
       
    10 # MiKael
       
    11 #
       
    12 
       
    13 if [[ $SHOWMODE != "on" ]]; then
       
    14     # Vi showmode is not enabled--skipping file
       
    15     return 0
       
    16 fi
       
    17 
       
    18 
       
    19 #####################
       
    20 # Vim-like showmode #
       
    21 #####################
       
    22 
       
    23 # vi keybindings
       
    24 bindkey -v
       
    25 
       
    26 bindkey -M vicmd "^R" redo
       
    27 bindkey -M vicmd "u" undo
       
    28 bindkey -M vicmd "ga" what-cursor-position
       
    29 
       
    30 unsetopt promptcr
       
    31 setopt transient_rprompt
       
    32 
       
    33 __string_insert="--INSERT--"
       
    34 __string_normal="--NORMAL--"
       
    35 __string_replace="--REPLACE-"
       
    36 
       
    37 redisplay-insert() {
       
    38    show_mode "$__string_insert"
       
    39 }
       
    40 redisplay-normal() {
       
    41    show_mode "$__string_normal"
       
    42 }
       
    43 zle -N redisplay-insert
       
    44 zle -N redisplay-normal
       
    45 bindkey -M viins "^X^R" redisplay-insert
       
    46 bindkey -M vicmd "^X^R" redisplay-normal
       
    47 
       
    48 screenclear () {
       
    49    echo -n "\033[2J\033[400H"
       
    50    builtin zle .redisplay
       
    51    show_mode "$__string_insert"
       
    52 }
       
    53 zle -N screenclear
       
    54 bindkey "" screenclear
       
    55 
       
    56 function zle-line-finish {
       
    57    export RPS1="$__string_insert"
       
    58 }
       
    59 zle -N zle-line-finish
       
    60 
       
    61 show_mode() {
       
    62    export RPS1=$1
       
    63    builtin zle reset-prompt
       
    64    return
       
    65 }
       
    66 
       
    67 
       
    68 ###       vi-add-eol (unbound) (A) (unbound)
       
    69 ###              Move  to the end of the line and enter insert mode.
       
    70 
       
    71 vi-add-eol() {
       
    72    show_mode "$__string_insert"
       
    73    builtin zle .vi-add-eol
       
    74 }
       
    75 zle -N vi-add-eol
       
    76 bindkey -M vicmd "A" vi-add-eol
       
    77 
       
    78 ###       vi-add-next (unbound) (a) (unbound)
       
    79 ###              Enter insert mode after the  current  cursor  posiĀ­
       
    80 ###              tion, without changing lines.
       
    81 vi-add-next() {
       
    82    show_mode "$__string_insert"
       
    83    builtin zle .vi-add-next
       
    84 }
       
    85 zle -N vi-add-next
       
    86 bindkey -M vicmd "a" vi-add-next
       
    87 
       
    88 
       
    89 ###       vi-change (unbound) (c) (unbound)
       
    90 ###              Read a movement command from the keyboard, and kill
       
    91 ###              from  the  cursor  position  to the endpoint of the
       
    92 ###              movement.  Then enter insert mode.  If the  command
       
    93 ###              is vi-change, change the current line.
       
    94 
       
    95 vi-change() {
       
    96    show_mode "$__string_insert"
       
    97    builtin zle .vi-change
       
    98 }
       
    99 zle -N vi-change
       
   100 bindkey -M vicmd "c" vi-change
       
   101 
       
   102 ###       vi-change-eol (unbound) (C) (unbound)
       
   103 ###              Kill  to the end of the line and enter insert mode.
       
   104 
       
   105 vi-change-eol() {
       
   106    show_mode "$__string_insert"
       
   107    builtin zle .vi-change-eol
       
   108 }
       
   109 zle -N vi-change-eol
       
   110 bindkey -M vicmd "C" vi-change-eol
       
   111 
       
   112 ###       vi-change-whole-line (unbound) (S) (unbound)
       
   113 ###              Kill the current line and enter insert mode.
       
   114 
       
   115 vi-change-whole-line() {
       
   116    show_mode "$__string_insert"
       
   117    builtin zle .vi-change-whole-line
       
   118 }
       
   119 zle -N vi-change-whole-line
       
   120 bindkey -M vicmd "S" vi-change-whole-line
       
   121 
       
   122 ###       vi-insert (unbound) (i) (unbound)
       
   123 ###              Enter insert mode.
       
   124 
       
   125 vi-insert() {
       
   126    show_mode "$__string_insert"
       
   127    builtin zle .vi-insert
       
   128 }
       
   129 zle -N vi-insert
       
   130 bindkey -M vicmd "i" vi-insert
       
   131 
       
   132 ###       vi-insert-bol (unbound) (I) (unbound)
       
   133 ###              Move to the first non-blank character on  the  line
       
   134 ###              and enter insert mode.
       
   135 
       
   136 vi-insert-bol() {
       
   137    show_mode "$__string_insert"
       
   138    builtin zle .vi-insert-bol
       
   139 }
       
   140 zle -N vi-insert-bol
       
   141 bindkey -M vicmd "I" vi-insert-bol
       
   142 
       
   143 ###       vi-open-line-above (unbound) (O) (unbound)
       
   144 ###              Open a line above the cursor and enter insert mode.
       
   145 
       
   146 vi-open-line-above() {
       
   147    show_mode "$__string_insert"
       
   148    builtin zle .vi-open-line-above
       
   149 }
       
   150 zle -N vi-open-line-above
       
   151 bindkey -M vicmd "O" vi-open-line-above
       
   152 
       
   153 ###       vi-open-line-below (unbound) (o) (unbound)
       
   154 ###              Open a line below the cursor and enter insert mode.
       
   155 
       
   156 vi-open-line-below() {
       
   157    show_mode "$__string_insert"
       
   158    builtin zle .vi-open-line-below
       
   159 }
       
   160 zle -N vi-open-line-below
       
   161 bindkey -M vicmd "o" vi-open-line-below
       
   162 
       
   163 ###       vi-substitute (unbound) (s) (unbound)
       
   164 ###              Substitute the next character(s).
       
   165 
       
   166 vi-substitute() {
       
   167    show_mode "$__string_insert"
       
   168    builtin zle .vi-substitute
       
   169 }
       
   170 zle -N vi-substitute
       
   171 bindkey -M vicmd "s" vi-substitute
       
   172 
       
   173 
       
   174 ###       vi-replace (unbound) (R) (unbound)
       
   175 ###              Enter overwrite mode.
       
   176 ###
       
   177 
       
   178 vi-replace() {
       
   179    show_mode "$__string_replace"
       
   180    builtin zle .vi-replace
       
   181 }
       
   182 zle -N vi-replace
       
   183 bindkey -M vicmd "R" vi-replace
       
   184 
       
   185 ###       vi-cmd-mode (^X^V) (unbound) (^[)
       
   186 ###              Enter  command  mode;  that  is, select the `vicmd'
       
   187 ###              keymap.  Yes, this is bound  by  default  in  emacs
       
   188 ###              mode.
       
   189 
       
   190 vi-cmd-mode() {
       
   191    show_mode "$__string_normal"
       
   192    builtin zle .vi-cmd-mode
       
   193 }
       
   194 zle -N vi-cmd-mode
       
   195 bindkey -M viins "" vi-cmd-mode
       
   196 
       
   197 
       
   198 ###       vi-oper-swap-case
       
   199 ###              Read a movement command from the keyboard, and swap
       
   200 ###              the case of all characters from the cursor position
       
   201 ###              to the endpoint of the movement.  If  the  movement
       
   202 ###              command  is vi-oper-swap-case, swap the case of all
       
   203 ###              characters on the current line.
       
   204 ###
       
   205 
       
   206 bindkey -M vicmd "g~" vi-oper-swap-case
       
   207 
       
   208 
       
   209 bindkey -M vicmd "[2~" vi-insert
       
   210 bindkey -M vicmd 'q'     push-line
       
   211 bindkey -M vicmd '[5~' history-beginning-search-backward # PgUp
       
   212 bindkey -M vicmd '[6~' history-beginning-search-forward  # PgDn
       
   213 
       
   214 bindkey -M viins '[5~' history-beginning-search-backward # PgUp
       
   215 bindkey -M viins '[6~' history-beginning-search-forward  # PgDn
       
   216 bindkey -M viins '[7~' beginning-of-line
       
   217 bindkey -M viins '[8~' end-of-line
       
   218 bindkey -M viins '^A'    beginning-of-line
       
   219 bindkey -M viins '^E'    end-of-line
       
   220 bindkey -M viins '[3~' delete-char
       
   221 bindkey -M viins ''  up-line-or-history
       
   222 bindkey -M viins ''  down-line-or-history
       
   223 bindkey -M viins '^Xc'   copy-prev-word
       
   224 bindkey -M viins '^Xf'   _correct_filename
       
   225 bindkey -M viins '^_'    undo
       
   226 bindkey -M viins '^K'    vi-change-eol
       
   227 bindkey -M viins '^R'    history-incremental-search-backward
       
   228 bindkey -M viins '^O'    accept-line-and-down-history
       
   229 bindkey -M viins '^T'    transpose-chars
       
   230 bindkey -M viins '^Xm'   _most_recent_file
       
   231 
       
   232 bindkey -M viins -s '^X^H' 'hash -r\n'
       
   233 
       
   234 
       
   235 if [[ -z ${TERM:#screen} ]]; then
       
   236   bindkey -M viins '[1~'  beginning-of-line   # Home
       
   237   bindkey -M viins '[4~'  end-of-line         # End
       
   238 fi
       
   239 
       
   240 # Edit the command line using your usual editor.
       
   241 autoload edit-command-line
       
   242 zle -N edit-command-line
       
   243 bindkey -M vicmd v edit-command-line
       
   244 
       
   245 export RPS1="$__string_insert"