From 5abc3e220407cdaac88e5cb33ff2a7b73a80d54f Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Mon, 9 May 2022 07:03:59 +0100 Subject: [PATCH 1/2] Avoid calling echo to expand fzf args ZSH_FZF_HISTORY_SEARCH_FZF_ARGS and ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS may contain multiple arguments separated by spaces that need to be expanded to build the command line for fzf. This is currently done by calling a subshell and letting echo handle it. Unlike other shells, zsh does not do word splitting by default. According to zshexpn(1) you can perform word splitting using the rules for SH_WORD_SPLIT by using the ${=spec} construct. Avoid creating a subshell and just expand the spaces. --- zsh-fzf-history-search.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-fzf-history-search.zsh b/zsh-fzf-history-search.zsh index 50876ff..5e4dbe7 100644 --- a/zsh-fzf-history-search.zsh +++ b/zsh-fzf-history-search.zsh @@ -19,7 +19,7 @@ typeset -g ZSH_FZF_HISTORY_SEARCH_END_OF_LINE='' fzf_history_search() { setopt extendedglob - candidates=(${(f)"$(fc -li -1 0 | fzf $(echo $ZSH_FZF_HISTORY_SEARCH_FZF_ARGS) $(echo $ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS) -q "$BUFFER")"}) + candidates=(${(f)"$(fc -li -1 0 | 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}[4,-1]}}" From 754d07402798d207aa6b48fd504ca8873bb296cd Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Mon, 9 May 2022 06:32:40 +0100 Subject: [PATCH 2/2] Add an option to remove event numbers from search Event numbers don't add information to the search: if you knew the event number you wouldn't be searching for the command. Add an option to remove them from the search to declutter it. Default it to true to preserve the current behaviour. --- README.md | 1 + zsh-fzf-history-search.zsh | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0157af0..33da056 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ plugins=(… zsh-fzf-history-search) | `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`)| ## TODO diff --git a/zsh-fzf-history-search.zsh b/zsh-fzf-history-search.zsh index 5e4dbe7..c128f82 100644 --- a/zsh-fzf-history-search.zsh +++ b/zsh-fzf-history-search.zsh @@ -17,12 +17,25 @@ typeset -g ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS='' (( ! ${+ZSH_FZF_HISTORY_SEARCH_END_OF_LINE} )) && typeset -g ZSH_FZF_HISTORY_SEARCH_END_OF_LINE='' +# Include event numbers +(( ! ${+ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS} )) && +typeset -g ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS=1 + fzf_history_search() { setopt extendedglob - candidates=(${(f)"$(fc -li -1 0 | fzf ${=ZSH_FZF_HISTORY_SEARCH_FZF_ARGS} ${=ZSH_FZF_HISTORY_SEARCH_FZF_EXTRA_ARGS} -q "$BUFFER")"}) + + FC_ARGS="-li" + CANDIDATE_LEADING_FIELDS=4 + + if (( ! $ZSH_FZF_HISTORY_SEARCH_EVENT_NUMBERS )); then + FC_ARGS+=" -n" + CANDIDATE_LEADING_FIELDS=3 + fi + + candidates=(${(f)"$(fc ${=FC_ARGS} -1 0 | 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}[4,-1]}}" + BUFFER="${candidates[@]/(#m)*/${${(As: :)MATCH}[${CANDIDATE_LEADING_FIELDS},-1]}}" BUFFER="${BUFFER[@]/(#b)(?)\\n/$match[1] }" zle vi-fetch-history -n $BUFFER