m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-19 09:03:43 -05:00

Updated Examples (markdown)

Matthew Bennett
2020-10-18 19:58:42 +02:00
parent b332d1b5bb
commit 4ef4057865

@@ -120,40 +120,80 @@ fo() {
} }
``` ```
Run command/application and choose paths/files with fzf.
Always return control of the termainal to user (e.g. when opening GUIs).
```sh ```sh
# Usage: #!/bin/bash
f cd # (hit enter, choose path)
f cat # (hit enter, choose files)
f vim # (hit enter, choose files)
f vlc # (hit enter, choose files)
# the function (not tested in zsh):
f() {
# store the files from fzf
IFS=$'\n' files=($(fzf --query="$2" --multi))
# if no files passed (e.g. if Esc pressed), return to terminal # Run command/application and choose paths/files with fzf.
if [ -z "${files}" ]; then # Always return control of the terminal to user (e.g. when opening GUIs).
# The full command that was used will appear in you history just like any other
# (N.B. to achieve this I write the shell's active history to ~/.bash_history)
#
# Usage:
# f cd (hit enter, choose path)
# f cat (hit enter, choose files)
# f vim (hit enter, choose files)
# f vlc (hit enter, choose files)
f() {
# Store the arguments from fzf
IFS=$'\n' arguments=($(fzf --query="$2" --multi))
# If no arguments passed (e.g. if Esc pressed), return to terminal
if [ -z "${arguments}" ]; then
return 1 return 1
fi fi
# send the files to a program # We want the command to show up in our bash history, so write the shell's
# some programs should not be run as background # active history to ~/.bash_history. Then we'll also add the command from
if ! [[ $1 =~ ^(cd)$ ]]; then # fzf, then we'll load it all back into the shell's active history
$1 "${files[@]}" & history -w
else
$1 "${files[@]}"
fi
# if the program is not on the list of GUIs (e.g. vim, cat, etc.) bring it # RUN THE COMMANDS ########################################################
# to foreground so we can see the output. Also put cd on this list. # The cd command has no effect when run as background, and doesn't show up
if ! [[ $1 =~ ^(cd|zathura|vlc)$ ]]; then # as a job the can be brought to the foreground. So we make sure not to add
# a & (more programs can be added separated by a '|')
if ! [[ $1 =~ ^(cd)$ ]]; then
$1 "${arguments[@]}" &
else
$1 "${arguments[@]}"
fi
# If the program is not on the list of GUIs (e.g. vim, cat, etc.) bring it
# to foreground so we can see the output. Also put cd on this list
# otherwise there will be errors)
if ! [[ $1 =~ ^(cd|zathura|vlc|eog|kolourpaint)$ ]]; then
fg %% fg %%
fi fi
}
# ADD A REPEATABLE COMMAND TO THE BASH HISTORY ############################
# Store the arguments in a temporary file for sanitising before being
# entered into bash history
: > ~/.fzf_tmp
for file in ${arguments[@]}; do
echo $file >> ~/.fzf_tmp
done
# Put all input arguments on one line and sanitise the command such that
# spaces and parentheses are properly escaped. More sanitisation
# substitutions can be added if needed
sed -i 's/\n//g; s/ /\\ /g; ${s/(/\\(/}; ${s/)/\\)/}' ~/.fzf_tmp
# If the program is on the GUI list add a '&' to the command history
if [[ $1 =~ ^(zathura|vlc|eog|kolourpaint)$ ]]; then
sed -i '${s/$/ \&/}' ~/.fzf_tmp
fi
# Grab the sanitised arguments
arguments=$(cat ~/.fzf_tmp)
# Add the command with the sanitised arguments to our .bash_history
echo ${1} ${arguments} >> ~/.bash_history
# Reload the ~/.bash_history into the shell's active history
history -r
# Clean up temporary variables
rm ~/.fzf_tmp
}
``` ```
```sh ```sh