Monday, December 30, 2013

Zsh History Search

Compared to Fish, currently I like Zsh a little bit more. But one of the unforgettable feature of Fish is its auto-suggestion based on history.
It made command line input so fluent.

Zsh has a feature similar to that called predict. You could configure by placing the following codes in your ~/zshrc.
You could press <Ctrl-Z> to toggle predict on and off.

autoload predict-on
predict-toggle() {
  ((predict_on=1-predict_on)) && predict-on || predict-off
}
zle -N predict-toggle
bindkey '^P'   predict-toggle
zstyle ':predict' toggle true
zstyle ':predict' verbose true

But compared to Fish’s auto-suggestion, I feel that the predict feature is less useful.
The difference show up when you simply type ls<Enter>.

  • In Fish, only ls will be executed.
  • In Zsh, if predict was turned on, ls -la will be executed, which is sometimes not what we want.

So for a more convenient way of accessing command line history, I prefer Zsh’s vi mode and history-search-backward.
You could press <Ctrl-J> and <Ctrl-K> to search history forward and backward.

# vi mode
setopt vi

# bind k and j for history search in vi mode
bindkey -M viins '^k' history-search-backward
bindkey -M viins '^j' history-search-forward
bindkey -M vicmd '^k' history-search-backward
bindkey -M vicmd '^j' history-search-forward

No comments:

Post a Comment