mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-19 09:03:43 -05:00
Updated Examples (markdown)
153
Examples.md
153
Examples.md
@@ -129,73 +129,94 @@ Suggested by [Matt-A-Bennett](https://github.com/Matt-A-Bennett) (not tested in
|
|||||||
# other (N.B. to achieve this I write the shell's active history to
|
# other (N.B. to achieve this I write the shell's active history to
|
||||||
# ~/.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() {
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# 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
|
f() {
|
||||||
# to foreground so we can see the output. Also put cd on this list
|
# Store the program
|
||||||
# otherwise there will be errors)
|
program="$1"
|
||||||
if ! [[ $1 =~ ^(cd|zathura|vlc|eog|kolourpaint)$ ]]; then
|
|
||||||
fg %%
|
# Remove first argument off the list
|
||||||
fi
|
shift
|
||||||
|
|
||||||
# ADD A REPEATABLE COMMAND TO THE BASH HISTORY ############################
|
# Store option flags
|
||||||
# Store the arguments in a temporary file for sanitising before being
|
options="$@"
|
||||||
# entered into bash history
|
|
||||||
: > /tmp/fzf_tmp
|
# Store the arguments from fzf
|
||||||
for file in ${arguments[@]}; do
|
arguments=($(fzf --multi))
|
||||||
echo $file >> /tmp/fzf_tmp
|
|
||||||
done
|
# If no arguments passed (e.g. if Esc pressed), return to terminal
|
||||||
|
if [ -z "${arguments}" ]; then
|
||||||
# Put all input arguments on one line and sanitise the command such that
|
return 1
|
||||||
# spaces and parentheses are properly escaped. More sanitisation
|
fi
|
||||||
# substitutions can be added if needed
|
|
||||||
sed -i 's/\n//g; s/ /\\ /g; s/(/\\(/; s/)/\\)/' /tmp/fzf_tmp
|
# 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
|
||||||
# If the program is on the GUI list add a '&' to the command history
|
# fzf, then we'll load it all back into the shell's active history
|
||||||
if [[ $1 =~ ^(zathura|vlc|eog|kolourpaint)$ ]]; then
|
history -w
|
||||||
sed -i '${s/$/ \&/}' /tmp/fzf_tmp
|
|
||||||
fi
|
# RUN THE COMMANDS ########################################################
|
||||||
|
# The cd command has no effect when run as background, and doesn't show up
|
||||||
# Grab the sanitised arguments
|
# as a job the can be brought to the foreground. So we make sure not to add
|
||||||
arguments=$(cat /tmp/fzf_tmp)
|
# a '&' (more programs can be added separated by a '|')
|
||||||
|
if ! [[ $program =~ ^(cd)$ ]]; then
|
||||||
# Add the command with the sanitised arguments to our .bash_history
|
if [ -z "$options" ]; then
|
||||||
echo ${1} ${arguments} >> ~/.bash_history
|
"$program" "${arguments[@]}" &
|
||||||
|
else
|
||||||
# Reload the ~/.bash_history into the shell's active history
|
"$program" "$options" "${arguments[@]}" &
|
||||||
history -r
|
fi
|
||||||
|
else
|
||||||
# Clean up temporary variables
|
if [ -z "$options" ]; then
|
||||||
rm /tmp/fzf_tmp
|
"$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 ! [[ "$program" =~ ^(cd|nautilus|zathura|evince|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
|
||||||
|
: > /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 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 [[ "$program" =~ ^(nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
|
||||||
|
sed -i '${s/$/ \&/}' /tmp/fzf_tmp
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Grab the sanitised arguments
|
||||||
|
arguments="$(cat /tmp/fzf_tmp)"
|
||||||
|
|
||||||
|
# Add the command with the sanitised arguments to our .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
|
||||||
|
|
||||||
|
# Clean up temporary variables
|
||||||
|
rm /tmp/fzf_tmp
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
Reference in New Issue
Block a user