Make de-duplication optional via ZSH_PECO_HISTORY_DEDUP env var

This commit is contained in:
2018-12-04 02:10:43 +00:00
parent 92cb0d5673
commit fed6b2db3a
2 changed files with 48 additions and 13 deletions

View File

@@ -20,6 +20,7 @@ snippet from [percol](https://github.com/mooz/percol).
- [peco](https://github.com/peco/peco)
- [psh](http://www.zsh.org/)
- `tac`, or `tail` with support for `-r` option.
- `perl`, `awk`, or `uniq` for if de-duplication is turned on.
## Installation
@@ -76,7 +77,35 @@ snippet from [percol](https://github.com/mooz/percol).
3. Start a new terminal session.
### Recommended Peco Config
## Configuration Options
### `ZSH_PECO_HISTORY_DEDUP`
De-duplicates all history entries before they are handed over to peco. Disabled
by default.
To enable, set the `ZSH_PECO_HISTORY_DEDUP` environment variable to a non-zero
value before zsh-peco-history is loaded.
This is useful if you often have a lot of duplicate entries when searching
through your history. However in that case it often enough to configure ZSH to
not save duplicate commands to history in the first place. This can be done by
setting the following options:
```
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_SAVE_NO_DUPS
```
[Oh My Zsh](https://github.com/robbyrussell/oh-my-zsh) for example sets the
above options, and more in it's
[`history.zsh`](https://github.com/mattjj/my-oh-my-zsh/blob/b1d4bab329456e9a4af49237064d9a3b6566f1b0/history.zsh)
file.
## Recommended Peco Config
You don't have to use this config, it's simply what I personally use. So here's
what my `~/.peco/config.json` file looks like:

View File

@@ -6,29 +6,35 @@
# Requirements:
# - peco: https://github.com/peco/peco
# - tac, or tail with support for -r option
# - perl, awk, or uniq for if de-duplication is turned on
#
# Based on: https://github.com/mooz/percol#zsh-history-search
if which peco &> /dev/null; then
function peco_select_history() {
local tac
local uniq
local parse_cmd
if (( $+commands[gtac] )); then
tac="gtac"
parse_cmd="gtac"
elif (( $+commands[tac] )); then
tac="tac"
parse_cmd="tac"
else
tac="tail -r"
parse_cmd="tail -r"
fi
if (( $+commands[perl] )); then
uniq="perl -ne 'print unless \$seen{\$_}++'"
elif (( $+commands[awk] )); then
uniq="awk '!seen[\$0]++'"
else
uniq="uniq"
if [ -n "$ZSH_PECO_HISTORY_DEDUP" ]; then
if (( $+commands[perl] )); then
parse_cmd="$parse_cmd | perl -ne 'print unless \$seen{\$_}++'"
elif (( $+commands[awk] )); then
parse_cmd="$parse_cmd | awk '!seen[\$0]++'"
else
parse_cmd="$parse_cmd | uniq"
fi
fi
BUFFER=$(fc -l -n 1 | eval $tac | eval $uniq | \
BUFFER=$(fc -l -n 1 | eval $parse_cmd | \
peco --layout=bottom-up --query "$LBUFFER")
CURSOR=$#BUFFER # move cursor
zle -R -c # refresh
}