Last active
March 16, 2016 19:59
-
-
Save pprince/6bdd4e46335fa2d87bab to your computer and use it in GitHub Desktop.
.zshrc -- Episode vi: A New Hope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ~/.zshrc | |
| # ======== | |
| # This file is sourced only for interactive shells. It | |
| # should contain commands to set up aliases, functions, | |
| # options, key bindings, etc. | |
| # | |
| # Global Order: zshenv, zprofile, zshrc, zlogin | |
| # ~83% of the following was shamelessly stolen from Debian 8's /etc/zsh/zshrc, | |
| # and another 14% was lifted from https://github.com/lackac/prezto | |
| ### <Keybindings> ### | |
| # Enable vi-like editing | |
| bindkey -v | |
| # Use the terminfo database to look-up what the current terminal sends | |
| # for each special keys we may want to bind, and store them for later. | |
| # ($key is here declared as an associative-array aka dictionary or hash-map.) | |
| typeset -A key | |
| key=( | |
| Control '\C-' | |
| Escape '\e' | |
| Meta '\M-' | |
| BackSpace "${terminfo[kbs]}" | |
| Up "${terminfo[kcuu1]}" | |
| Down "${terminfo[kcud1]}" | |
| Left "${terminfo[kcub1]}" | |
| Right "${terminfo[kcuf1]}" | |
| Home "${terminfo[khome]}" | |
| End "${terminfo[kend]}" | |
| Insert "${terminfo[kich1]}" | |
| Delete "${terminfo[kdch1]}" | |
| PageUp "${terminfo[kpp]}" | |
| PageDown "${terminfo[knp]}" | |
| BackTab "${terminfo[kcbt]}" | |
| 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]}" | |
| ) | |
| # handy function; it lets you set bindings for multiple modes at once, i.e.: | |
| # emacs (zsh default), viins (vi insert-mode), and vicmd (vi command-mode). | |
| function bind2maps () { | |
| local i sequence widget | |
| local -a maps | |
| while [[ "$1" != "--" ]]; do | |
| maps+=( "$1" ) | |
| shift | |
| done | |
| shift | |
| sequence="${key[$1]}" | |
| widget="$2" | |
| [[ -z "$sequence" ]] && return 1 | |
| for i in "${maps[@]}"; do | |
| bindkey -M "$i" "$sequence" "$widget" | |
| done | |
| } | |
| # Now we use it, to declare most of the same bindings that Debian had in | |
| # their /etc/zsh/zshrc: | |
| bind2maps emacs -- BackSpace backward-delete-char | |
| bind2maps viins -- BackSpace vi-backward-delete-char | |
| bind2maps vicmd -- BackSpace vi-backward-char | |
| bind2maps emacs -- Home beginning-of-line | |
| bind2maps viins vicmd -- Home vi-beginning-of-line | |
| bind2maps emacs -- End end-of-line | |
| bind2maps viins vicmd -- End vi-end-of-line | |
| bind2maps emacs viins -- Insert overwrite-mode | |
| bind2maps vicmd -- Insert vi-insert | |
| bind2maps emacs -- Delete delete-char | |
| bind2maps viins vicmd -- Delete vi-delete-char | |
| bind2maps emacs -- Left backward-char | |
| bind2maps viins vicmd -- Left vi-backward-char | |
| bind2maps emacs -- Right forward-char | |
| bind2maps viins vicmd -- Right vi-forward-char | |
| # But also our new ones for searching the history. (However, we'll fall-back | |
| # to vanilla behavior, if "{up,down}-line-or-beginning" search fails somehow.) | |
| if autoload -Uz up-line-or-beginning-search && zle -N $_ && \ | |
| autoload -Uz down-line-or-beginning-search && zle -N $_ | |
| then | |
| bind2maps emacs viins vicmd -- Up up-line-or-beginning-search | |
| bind2maps emacs viins vicmd -- Down down-line-or-beginning-search | |
| else | |
| bind2maps emacs viins vicmd -- Up up-line-or-history | |
| bind2maps emacs viins vicmd -- Down down-line-or-history | |
| fi | |
| # see: http://imgur.com/eeydewp | |
| unfunction bind2maps | |
| ### </keybindings> ### | |
| # "Editors are expected to switch to "application" mode using the "smkx" | |
| # terminfo capability when they start, and to go back to the normal "cursor" | |
| # mode ("rmkx") when they exit." -- otherwise, terminfo isn't valid/reliable. | |
| if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then | |
| function zle-line-init () { | |
| emulate -L zsh | |
| printf '%s' ${terminfo[smkx]} | |
| } | |
| function zle-line-finish () { | |
| emulate -L zsh | |
| printf '%s' ${terminfo[rmkx]} | |
| } | |
| zle -N zle-line-init | |
| zle -N zle-line-finish | |
| fi | |
| # this seems kindof useful: if we have a $PAGER set, tell Zsh to use it; | |
| # otherwise fall-back to `more` (which is Zsh's default here anyway.) | |
| READNULLCMD=${PAGER:-more} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment