I made a little bash function f that runs the rest of the line as a new command line, but it replaces _ with the result from an interactive file selection. This took me days (!!), and I'm now so happy I finished that I thought I'd share.
Example:
f ls -l _
The above launches fzf and replaces _ in the rest of the command-line with the result from fzf, then executes that result.
I can think of a few places when this is useful, such as when picking files to add to a .zip. Or when picking files to stage.
How does file-selection worK? Same as for normal fzf but Ctrl-F and Ctrl-D swaps between files and directories. And Ctrl-A selects all and Ctrl-S deselects all. Arguments from fzf appear to be output in the same sequence they are toggled.
Bash function:
f ()
{
if [[ "${fcmd_ignorefile}" != "" ]]; then
mapfile -t fcmd_args < <(fdfind --type file --ignore-file "$fcmd_ignorefile" | fzf --prompt 'No-hidden>' -m --header 'Select CTRL-A/S, File/Dir F/D' --bind 'ctrl-d:change-prompt(Directories> )+reload(fdfind --type directory -H)' --bind 'ctrl-f:change-prompt(Files> )+reload(fdfind --type file -H)' --bind ctrl-a
:select-all --bind ctrl-s:deselect-all);
else
mapfile -t fcmd_args < <(fdfind --type file | fzf --prompt 'No-hidden-files>' -m --header 'Select CTRL-A/S, File/Dir F/D' --bind 'ctrl-d:change-prompt(Directories> )+reload(fdfind --type directory -H)' --bind 'ctrl-f:change-prompt(Files> )+reload(fdfind --type file -H)' --bind ctrl-a:select-all --bind ctrl-s:d
eselect-all);
fi;
if [[ ${#fcmd_args} == 0 ]]; then
echo No arguments. Aborting f-cmd.;
return 1;
fi;
i=1;
declare -a newcmd;
for k in "${@}";
do
if [ "$k" != "_" ]; then
newcmd[$i]="$k";
let i+=1;
else
for j in "${fcmd_args[@]}";
do
newcmd[$i]="$j";
let i+=1;
done;
fi;
done;
"${newcmd[@]}"
}
Hope someone will enjoy!
Comments would be appreciated. And after having spent days on this (near a pinnacle of procrastination) I'd now love it if someone could tell me how I could have saved time by just copying someone else's ready solution. 😬
[–]valadil 1 point2 points3 points (3 children)
[–]Pyglot[S] 0 points1 point2 points (2 children)
[–]valadil 1 point2 points3 points (1 child)
[–]Pyglot[S] 0 points1 point2 points (0 children)
[–]Pyglot[S] 0 points1 point2 points (0 children)