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
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)
#
# 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 cd [OPTIONS] (hit enter, choose path)
# f cat [OPTIONS] (hit enter, choose files)
# f vim [OPTIONS] (hit enter, choose files)
# f vlc [OPTIONS] (hit enter, choose files)
f() {
# Store the program
program="$1"
# Remove first argument off the list
shift
# Store option flags
options="$@"
# 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 [ -z "${arguments}" ]; then
@@ -153,16 +162,24 @@ f() {
# 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[@]}" &
if ! [[ $program =~ ^(cd)$ ]]; then
if [ -z "$options" ]; then
"$program" "${arguments[@]}" &
else
"$program" "$options" "${arguments[@]}" &
fi
else
$1 "${arguments[@]}"
if [ -z "$options" ]; then
"$program" "${arguments[@]}"
else
"$program" "$options" "${arguments[@]}"
fi
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
if ! [[ "$program" =~ ^(cd|nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
fg %%
fi
@@ -170,25 +187,29 @@ f() {
# Store the arguments in a temporary file for sanitising before being
# entered into bash history
: > /tmp/fzf_tmp
for file in ${arguments[@]}; do
echo $file >> /tmp/fzf_tmp
for file in "${arguments[@]}"; do
echo "$file" >> /tmp/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/)/\\)/' /tmp/fzf_tmp
# Put all input arguments on one line and sanitise the command by putting
# single quotes around each argument, also first put an extra single quote
# next to any pre-existing single quotes in the raw argument
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 [[ $1 =~ ^(zathura|vlc|eog|kolourpaint)$ ]]; then
if [[ "$program" =~ ^(nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
sed -i '${s/$/ \&/}' /tmp/fzf_tmp
fi
# 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
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
history -r