Просмотр исходного кода

Enable zsh up arrow search alongside vim bindings

We have cobbled together bits of `oh-my-zsh/lib/keybindings.zsh` and
`oh-my-zsh/plugins/vi-mode.zsh` to get the best of both worlds: up arrow and
ctrl-r searching along with zle vicmd mappings.
Weiyi Lou 10 лет назад
Родитель
Сommit
60c3422a79
4 измененных файлов с 61 добавлено и 4 удалено
  1. 7 0
      zsh/custom/cinaeco.zsh-theme
  2. 3 3
      zsh/custom/history.zsh
  3. 50 0
      zsh/custom/lib/key-bindings.zsh
  4. 1 1
      zsh/zshrc

+ 7 - 0
zsh/custom/cinaeco.zsh-theme

@@ -153,3 +153,10 @@ function parse_git_dirty() {
     echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
   fi
 }
+
+# Show if a prompt is in vi command mode.
+#
+# Relies on ZLE widget `zle-keymap-select` in `keybindings.zsh`.
+function vi_mode_prompt_info() {
+  echo "${${KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/}"
+}

+ 3 - 3
zsh/custom/history.zsh

@@ -1,6 +1,6 @@
-## history
+# Quick history
 alias h='history 100'
-## don't show expansions on return, just execute, e.g. for !! and !$
+# Don't show expansions on return, just execute, e.g. for !! and !$
 setopt nohistverify
-## don't have the same history across tabs/windows
+# Don't have the same history across tabs/windows
 setopt nosharehistory

+ 50 - 0
zsh/custom/lib/key-bindings.zsh

@@ -0,0 +1,50 @@
+# Overridden keybindings to have up arrow history matching and vi-mode.
+#
+# vi-mode plugin and oh-my-zsh's key-binding library don't match well, because
+# of the way `bindkey -v` overrides previously made bindings.
+#
+# Unfortunately, there is no nice way to reconcile the two, so parts of both
+# have been extracted and placed here.
+
+# Make sure that the terminal is in application mode when zle is active, since
+# only then values from $terminfo are valid
+if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
+  function zle-line-init() {
+    echoti smkx
+  }
+  function zle-line-finish() {
+    echoti rmkx
+  }
+  zle -N zle-line-init
+  zle -N zle-line-finish
+fi
+
+# From vi-mode: needed to get prompt indicator to work in `cinaeco.zsh-theme`
+function zle-keymap-select {
+  # The terminal must be in application mode when ZLE is active for $terminfo
+  # values to be valid.
+  if (( ${+terminfo[smkx]} )); then
+    printf '%s' ${terminfo[smkx]}
+  fi
+  if (( ${+terminfo[rmkx]} )); then
+    printf '%s' ${terminfo[rmkx]}
+  fi
+
+  zle reset-prompt
+  zle -R
+}
+zle -N zle-keymap-select
+
+# Start with vim bindings
+bindkey -v
+
+# Key bindings for history search
+bindkey '^r' history-incremental-search-backward      # [Ctrl-r] - Search backward incrementally for a specified string. The string may begin with ^ to anchor the search to the beginning of the line.
+
+if [[ "${terminfo[kcuu1]}" != "" ]]; then
+  bindkey "${terminfo[kcuu1]}" up-line-or-search      # start typing + [Up-Arrow] - fuzzy find history forward
+fi
+
+if [[ "${terminfo[kcud1]}" != "" ]]; then
+  bindkey "${terminfo[kcud1]}" down-line-or-search    # start typing + [Down-Arrow] - fuzzy find history backward
+fi

+ 1 - 1
zsh/zshrc

@@ -46,7 +46,7 @@ ZSH_CUSTOM=$HOME/dotfiles/zsh/custom
 # Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
 # Example format: plugins=(rails git textmate ruby lighthouse)
 # Add wisely, as too many plugins slow down shell startup.
-plugins=(git vi-mode colored-man z)
+plugins=(git colored-man z)
 
 # User configuration