From 76c6588204189f74d9a7e0c6c7e2aaaf7e701b82 Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Thu, 12 May 2022 07:01:53 +0100 Subject: [PATCH 1/3] Add an option to remove dates from search Let the user decide if they want full date timestamps in ISO8601 format in the search. --- README.md | 1 + zsh-fzf-history-search.zsh | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 33da056..7deff00 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ plugins=(… zsh-fzf-history-search) | `ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS` | Extra arguments for `fzf` (default: `''`) | | `ZSH_FZF_HISTORY_SEARCH_END_OF_LINE` | Put the cursor on at the end of the line after completion, `empty=false` (default: `''`) | | `ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS` | Include event numbers in search. Set to 0 to remove event numbers from the search. (default: `1`)| +| `ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH` | Include ISO8601 timestamps in search. Set to 0 to remove them from the search. (default: `1`) | ## TODO diff --git a/zsh-fzf-history-search.zsh b/zsh-fzf-history-search.zsh index c128f82..264aa32 100644 --- a/zsh-fzf-history-search.zsh +++ b/zsh-fzf-history-search.zsh @@ -21,15 +21,24 @@ typeset -g ZSH_FZF_HISTORY_SEARCH_END_OF_LINE='' (( ! ${+ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS} )) && typeset -g ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS=1 +# Include full date timestamps in ISO8601 `yyyy-mm-dd hh:mm' format +(( ! ${+ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH} )) && +typeset -g ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH=1 + fzf_history_search() { setopt extendedglob - FC_ARGS="-li" - CANDIDATE_LEADING_FIELDS=4 + FC_ARGS="-l" + CANDIDATE_LEADING_FIELDS=2 if (( ! $ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS )); then FC_ARGS+=" -n" - CANDIDATE_LEADING_FIELDS=3 + ((CANDIDATE_LEADING_FIELDS--)) + fi + + if (( $ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH )); then + FC_ARGS+=" -i" + ((CANDIDATE_LEADING_FIELDS+=2)) fi candidates=(${(f)"$(fc ${=FC_ARGS} -1 0 | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"}) From 44b785e81bc94e4bab1d681193336ca60e7200f6 Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Thu, 12 May 2022 07:42:17 +0100 Subject: [PATCH 2/3] Add an option to remove duplicates from search Some users may prefer to keep duplicates in their zsh history, but drop them from search. The awk manual[0] recommends this. [0] https://www.gnu.org/software/gawk/manual/html_node/History-Sorting.html --- README.md | 1 + zsh-fzf-history-search.zsh | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7deff00..8c8d8e7 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ plugins=(… zsh-fzf-history-search) | `ZSH_FZF_HISTORY_SEARCH_END_OF_LINE` | Put the cursor on at the end of the line after completion, `empty=false` (default: `''`) | | `ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS` | Include event numbers in search. Set to 0 to remove event numbers from the search. (default: `1`)| | `ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH` | Include ISO8601 timestamps in search. Set to 0 to remove them from the search. (default: `1`) | +| `ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES`| Remove duplicate entries from search. Only makes sense with `EVENT_NUMBERS` and `DATE_INSEARCH` 0 (false). (default: ``) | ## TODO diff --git a/zsh-fzf-history-search.zsh b/zsh-fzf-history-search.zsh index 264aa32..84a0b18 100644 --- a/zsh-fzf-history-search.zsh +++ b/zsh-fzf-history-search.zsh @@ -25,6 +25,10 @@ typeset -g ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS=1 (( ! ${+ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH} )) && typeset -g ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH=1 +# Remove duplicate entries in history +(( ! ${+ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES} )) && +typeset -g ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES='' + fzf_history_search() { setopt extendedglob @@ -41,7 +45,18 @@ fzf_history_search() { ((CANDIDATE_LEADING_FIELDS+=2)) fi - candidates=(${(f)"$(fc ${=FC_ARGS} -1 0 | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"}) + history_cmd="fc ${=FC_ARGS} -1 0" + + if [ -n "${ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES}" ];then + if (( $+commands[awk] )); then + history_cmd="$history_cmd | awk '!seen[\$0]++'" + else + # In case awk is not installed fallback to uniq. It will only remove commands that are repeated consecutively. + history_cmd="$history_cmd | uniq" + fi + fi + + candidates=(${(f)"$(eval $history_cmd | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"}) local ret=$? if [ -n "$candidates" ]; then BUFFER="${candidates[@]/(#m)*/${${(As: :)MATCH}[${CANDIDATE_LEADING_FIELDS},-1]}}" From 83666d07225f18b2e478ceb2bfbfe7c94150431a Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Fri, 13 May 2022 07:29:38 +0100 Subject: [PATCH 3/3] Add a column to the variable documentation to show defaults. --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8c8d8e7..6dbf435 100644 --- a/README.md +++ b/README.md @@ -36,15 +36,15 @@ plugins=(… zsh-fzf-history-search) ## Configuration Variables -| Variable | Description | -| ----------------------------------------- | ------------------------------------------------------------------------------------------------- | -| `ZSH_FZF_HISTORY_SEARCH_BIND` | Keybind to trigger fzf reverse search (default: `'^r'`) | -| `ZSH_FZF_HISTORY_SEARCH_FZF_ARGS` | Arguments for `fzf` (might be updated, not recommended to override) (default: `'+s +m -x -e'`) | -| `ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS` | Extra arguments for `fzf` (default: `''`) | -| `ZSH_FZF_HISTORY_SEARCH_END_OF_LINE` | Put the cursor on at the end of the line after completion, `empty=false` (default: `''`) | -| `ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS` | Include event numbers in search. Set to 0 to remove event numbers from the search. (default: `1`)| -| `ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH` | Include ISO8601 timestamps in search. Set to 0 to remove them from the search. (default: `1`) | -| `ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES`| Remove duplicate entries from search. Only makes sense with `EVENT_NUMBERS` and `DATE_INSEARCH` 0 (false). (default: ``) | +| Variable | Default | Description | +| ----------------------------------------- | --------------- | ---------------------------------------------------------------------------------------------------------- | +| `ZSH_FZF_HISTORY_SEARCH_BIND` | `'^r'` | Keybind to trigger fzf reverse search | +| `ZSH_FZF_HISTORY_SEARCH_FZF_ARGS` | `'+s +m -x -e'` | Arguments for `fzf` (might be updated, not recommended to override) | +| `ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS` | `''` | Extra arguments for `fzf` | +| `ZSH_FZF_HISTORY_SEARCH_END_OF_LINE` | `''` | Put the cursor on at the end of the line after completion, `empty=false` | +| `ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS` | `1` | Include event numbers in search. Set to 0 to remove event numbers from the search. | +| `ZSH_FZF_HISTORY_SEARCH_DATES_IN_SEARCH` | `1` | Include ISO8601 timestamps in search. Set to 0 to remove them from the search. | +| `ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATES`| `''` | Remove duplicate entries from search. Only makes sense with `EVENT_NUMBERS` and `DATE_INSEARCH` 0 (false).| ## TODO