m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-20 09:33:42 -05:00

Updated Examples (markdown)

Matthew Bennett
2021-04-17 23:55:31 +02:00
parent 5d05056629
commit dd8398f6ba

@@ -130,14 +130,23 @@ Suggested by [Matt-A-Bennett](https://github.com/Matt-A-Bennett) (not tested in
# ~/.bash_history) # ~/.bash_history)
# #
# Usage: # Usage:
# f cd (hit enter, choose path) # f cd [OPTIONS] (hit enter, choose path)
# f cat (hit enter, choose files) # f cat [OPTIONS] (hit enter, choose files)
# f vim (hit enter, choose files) # f vim [OPTIONS] (hit enter, choose files)
# f vlc (hit enter, choose files) # f vlc [OPTIONS] (hit enter, choose files)
f() { f() {
# Store the program
program="$1"
# Remove first argument off the list
shift
# Store option flags
options="$@"
# Store the arguments from fzf # Store the arguments from fzf
IFS=$'\n' arguments=($(fzf --query="$2" --multi)) arguments=($(fzf --multi))
# If no arguments passed (e.g. if Esc pressed), return to terminal # If no arguments passed (e.g. if Esc pressed), return to terminal
if [ -z "${arguments}" ]; then if [ -z "${arguments}" ]; then
@@ -153,16 +162,24 @@ f() {
# The cd command has no effect when run as background, and doesn't show up # The cd command has no effect when run as background, and doesn't show up
# as a job the can be brought to the foreground. So we make sure not to add # 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 '|') # a '&' (more programs can be added separated by a '|')
if ! [[ $1 =~ ^(cd)$ ]]; then if ! [[ $program =~ ^(cd)$ ]]; then
$1 "${arguments[@]}" & if [ -z "$options" ]; then
"$program" "${arguments[@]}" &
else else
$1 "${arguments[@]}" "$program" "$options" "${arguments[@]}" &
fi
else
if [ -z "$options" ]; then
"$program" "${arguments[@]}"
else
"$program" "$options" "${arguments[@]}"
fi
fi fi
# If the program is not on the list of GUIs (e.g. vim, cat, etc.) bring it # 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 # to foreground so we can see the output. Also put cd on this list
# otherwise there will be errors) # otherwise there will be errors)
if ! [[ $1 =~ ^(cd|zathura|vlc|eog|kolourpaint)$ ]]; then if ! [[ "$program" =~ ^(cd|nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
fg %% fg %%
fi fi
@@ -170,25 +187,29 @@ f() {
# Store the arguments in a temporary file for sanitising before being # Store the arguments in a temporary file for sanitising before being
# entered into bash history # entered into bash history
: > /tmp/fzf_tmp : > /tmp/fzf_tmp
for file in ${arguments[@]}; do for file in "${arguments[@]}"; do
echo $file >> /tmp/fzf_tmp echo "$file" >> /tmp/fzf_tmp
done done
# Put all input arguments on one line and sanitise the command such that # Put all input arguments on one line and sanitise the command by putting
# spaces and parentheses are properly escaped. More sanitisation # single quotes around each argument, also first put an extra single quote
# substitutions can be added if needed # next to any pre-existing single quotes in the raw argument
sed -i 's/\n//g; s/ /\\ /g; s/(/\\(/; s/)/\\)/' /tmp/fzf_tmp sed -i "s/'/''/g; s/.*/'&'/g; s/\n//g" /tmp/fzf_tmp
# If the program is on the GUI list add a '&' to the command history # If the program is on the GUI list add a '&' to the command history
if [[ $1 =~ ^(zathura|vlc|eog|kolourpaint)$ ]]; then if [[ "$program" =~ ^(nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
sed -i '${s/$/ \&/}' /tmp/fzf_tmp sed -i '${s/$/ \&/}' /tmp/fzf_tmp
fi fi
# Grab the sanitised arguments # Grab the sanitised arguments
arguments=$(cat /tmp/fzf_tmp) arguments="$(cat /tmp/fzf_tmp)"
# Add the command with the sanitised arguments to our .bash_history # Add the command with the sanitised arguments to our .bash_history
echo ${1} ${arguments} >> ~/.bash_history if [ -z "$options" ]; then
echo $program $arguments >> ~/.bash_history
else
echo $program $options $arguments >> ~/.bash_history
fi
# Reload the ~/.bash_history into the shell's active history # Reload the ~/.bash_history into the shell's active history
history -r history -r