Ctrl-r for shell history but also bulk select and remove from history #3629
-
Ctrl-r is super useful for shell history but along with Enter to insert the command to the prompt, I would like (within the same Ctrl-r interface, preferably) to also switch to interface where selecting an entry removes it from history. It should support selecting multiple entries and repeat filtering to select additional entries to remove (only except with e.g. Ctrl-c). Any ideas on how to implement this if it's not implemented already? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
This setup requires 3 things:
Note The values assigned to If you place everything in your
More details on how the deletion works can be found in: #3522 modified-fzf-history-widget() {
local selected
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases no_bash_rematch 2> /dev/null
# appends the current shell history buffer to the HISTFILE
builtin fc -AI $HISTFILE
# pushes entries from the $HISTFILE onto a stack and uses this history
builtin fc -p $HISTFILE $HISTSIZE $SAVEHIST
selected="$(builtin fc -rl 1 |
awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,ctrl-z:ignore ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} --multi" $(__fzfcmd))"
local ret=$?
if [[ -n $selected ]]; then
if [[ "$selected" =~ ^[[:blank:]]*[[:digit:]]+ ]]; then
builtin fc -pa "$HISTFILE"
zle vi-fetch-history -n "$MATCH"
else # selected is a custom query, not from history
LBUFFER="$selected"
fi
fi
# Read the history from the history file into the history list
builtin fc -R $HISTFILE
zle reset-prompt
return $ret
}
zle -N modified-fzf-history-widget
bindkey "^R" modified-fzf-history-widget
export FZF_CTRL_R_OPTS="$(
cat <<'FZF_FTW'
--bind "ctrl-d:execute-silent(zsh -ic 'builtin fc -p $HISTFILE $HISTSIZE $SAVEHIST; for i in {+1}; do ignore+=( \"${(b)history[$i]}\" );done;
HISTORY_IGNORE=\"(${(j:|:)ignore})\";builtin fc -W $HISTFILE')+reload:builtin fc -p $HISTFILE $HISTSIZE $SAVEHIST; builtin fc -rl 1 |
awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, \"\", cmd); if (!seen[cmd]++) print $0 }'"
--bind 'enter:accept-or-print-query'
--header 'enter select · ^d remove'
--prompt ' Global History > '
FZF_FTW
)"
# 'ZDOTDIR' is a Parameter used by the shell, it refers to the location of your
# shell startup files (see 'man zshparam')
export HISTFILE="${ZDOTDIR:-$HOME}"/.zsh_history
export HISTSIZE=12000
export SAVEHIST=10000 EDIT: use |
Beta Was this translation helpful? Give feedback.
This setup requires 3 things:
fzf-history-widget
FZF_CTRL_R_OPTS
environment variable with a complex keybinding for deletion andreloading
HISTFILE
,HISTSIZE
, andSAVEHIST
environment variablesNote
The values assigned to
FZF_CTRL_R_OPTS
can also be incorporated into the modifiedfzf-history-widget
. However, I found this method simpler to avoid complications with quotationmarks.
If you place everything in your
.zshrc
, after sourcing thefzf/shell/key-bindings.zsh
file,it should work seamlessly. The history will always be read from your
$HISTFILE
, thus I also setthese options: