fzf icon indicating copy to clipboard operation
fzf copied to clipboard

Missing action to add entry to history manually

Open doak opened this issue 4 years ago • 7 comments

  • [x] I have read through the manual page (man fzf)
  • [x] I have the latest version of fzf
  • [x] I have searched through the existing issues

Info

  • OS
    • [x] Linux
    • [ ] Mac OS X
    • [ ] Windows
    • [ ] Etc.
  • Shell
    • [x] bash
    • [ ] zsh
    • [ ] fish

Problem / Steps to reproduce

I am using --bind="execute(echo {})+clear-query" --history=histfile to repeatedly print current selected entry without closing fzf. This works well, but I found no way to get these "accepted" entries into the history. (I am aware that I could add current query to histfile by myself as a workaround, but this breaks abstraction in my case. Furthermore are these entries only available at next restart of fzf.)

Do I miss something? If not, this would transform into a feature request for an action to add stuff to history manually (e.g. append-to-history({q})).

doak avatar May 11 '20 12:05 doak

Just a suggestion haven’t tested it yet but have you considered

—bind=somekey:replace-query+execute(echo {q} | tee -a >(/path/to/histfile))

Splits stdio to file (in this case) and stdout the -a is append instead of overwrite should work and if you want to manually type it in just remove replace the replace-query or remove enter your field and echo tee

rayiik avatar Aug 25 '20 16:08 rayiik

Hey @dsimon28, I'm sure if you write directly to the histfile it would appear as a history "modified" (it's marked with an "*") and won't make a valid command to come back.

I would love to have this as a feature, I have been fighting to make print -s working with fzf inside a zsh script and I got 0 success.


EDIT: I make it work using an alias to the script and running the script with "source".

The minimal script (called whatever.zsh)

#!/bin/zsh
$selected_script="jq .scripts | sed '1d;$d' | fzf";
print -s "yarn run "$selected_script;
alias run="source whatever.zsh"

The problem was while running print -s under a non-interactable script (or a not callable terminal) print -s would store the history into it's own history file and not the zsh system one, with source make the process run on the current process and they share history/exports etc.

davesnx avatar Sep 03 '20 08:09 davesnx

Question are you using zsh or bash? Don’t know how to do this in zsh but you could probably achieve the same effect with a here doc you or redirection but alias magic is good too I just know in bash you can’t easily pas positional parameters to an alias

rayiik avatar Sep 20 '20 10:09 rayiik

the reason I ask is that when you source a file outside your init file it can cause unexpected changes to your other environmental variables and in some cases break programs (the reason I ask the previous question is that you marked in the post that your using bash, but clearly using a zsh script) however i was able to get it to work for bash and you may want to just run it as a bash script from zsh

set -o nounset                              # Treat unset variables as an error
unset file
coms () {
case "$PATH" in
  (*[!:]:) PATH="$PATH:" ;;
esac

set -f; IFS=:
for dir in $PATH; do
  set +f
  [ -z "$dir" ] && dir="."
  for file in "$dir"/*; do
    if [ -x "$file" ] && ! [ -d "$file" ]; then
	    printf '%s = %s\n' "${file##*/}" "$file"
    fi
  done
done
}
_fline () {
  printf "%${COLUMNS:-137}s" " "  | tr " " "-"
}

selected="$(
coms | awk '{print $1}'  \
    | fzf --preview="(man {} || {} help); tail ~/.bash_history" \
    --bind="f8:replace-query" \
    --bind="f9:execute:echo {q} \
   | tee -a ~/.bash_history" \
)"
_selected="$selected"
man ${_selected} | fzf --preview="echo ${_selected} {q}; fline; echo \$("${_selected}" {q} | tr ' ' '\r') " \
--preview-window="top:35%:wrap" \
    --bind="f8:replace-query" \
 --bind="f9:execute:echo {q} \
   | tee -a ~/.bash_history" \
--phony

will print to history directly on F9 without wich you can see in the preview if you change the line your on this will let you build a command but it prints your commands then it lets you build a command then print on f9

rayiik avatar Sep 26 '20 01:09 rayiik

I found this issue and it could be quite helpful indeed, is this something that you would accept as a pull request @junegunn ?

edi9999 avatar Feb 01 '22 14:02 edi9999

up @junegunn ? Is this something that you would accept as a pull request ?

edi9999 avatar Sep 15 '22 14:09 edi9999

The action append-to-history({q})) seems appropriate,a suggested by @doak

edi9999 avatar Sep 15 '22 14:09 edi9999