From 4ef4057865554a9d1341a7a0bdce231d1ccbb4d9 Mon Sep 17 00:00:00 2001 From: Matthew Bennett Date: Sun, 18 Oct 2020 19:58:42 +0200 Subject: [PATCH] Updated Examples (markdown) --- Examples.md | 92 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/Examples.md b/Examples.md index 1215c02..c8445aa 100644 --- a/Examples.md +++ b/Examples.md @@ -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 -# 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) - -# the function (not tested in zsh): -f() { - # store the files from fzf - IFS=$'\n' files=($(fzf --query="$2" --multi)) +#!/bin/bash - # if no files passed (e.g. if Esc pressed), return to terminal - if [ -z "${files}" ]; then +# Run command/application and choose paths/files with fzf. +# 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 fi - # send the files to a program - # some programs should not be run as background - if ! [[ $1 =~ ^(cd)$ ]]; then - $1 "${files[@]}" & - else - $1 "${files[@]}" - fi + # We want the command to show up in our bash history, so write the shell's + # active history to ~/.bash_history. Then we'll also add the command from + # fzf, then we'll load it all back into the shell's active history + history -w - # 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. - if ! [[ $1 =~ ^(cd|zathura|vlc)$ ]]; then + # RUN THE COMMANDS ######################################################## + # 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 + # 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 %% 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