mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-19 00:53:42 -05:00
Updated Examples (markdown)
187
Examples.md
187
Examples.md
@@ -61,104 +61,6 @@ Table of Contents
|
||||
|
||||
|
||||
### General
|
||||
Suggested by [Matt-A-Bennett](https://github.com/Matt-A-Bennett) (not tested in zsh):
|
||||
```sh
|
||||
# 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 your history just like any
|
||||
# other (N.B. to achieve this I write the shell's active history to
|
||||
# ~/.bash_history)
|
||||
#
|
||||
# Usage:
|
||||
# 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
|
||||
arguments=($(fzf --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 ! [[ $program =~ ^(cd)$ ]]; then
|
||||
if [ -z "$options" ]; then
|
||||
"$program" "${arguments[@]}" &
|
||||
else
|
||||
"$program" "$options" "${arguments[@]}" &
|
||||
fi
|
||||
else
|
||||
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 ! [[ "$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
|
||||
# Use fd and fzf to get the args to a command.
|
||||
# Works only with zsh
|
||||
@@ -195,6 +97,95 @@ ec () {
|
||||
}
|
||||
```
|
||||
|
||||
Inspired by the above, suggested by [Matt-A-Bennett](https://github.com/Matt-A-Bennett) (not tested in zsh):
|
||||
```sh
|
||||
# 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 your history just like any
|
||||
# other (N.B. to achieve this I write the shell's active history to
|
||||
# ~/.bash_history)
|
||||
#
|
||||
# Usage:
|
||||
# f cd [OPTION]... (hit enter, choose path)
|
||||
# f cat [OPTION]... (hit enter, choose files)
|
||||
# f vim [OPTION]... (hit enter, choose files)
|
||||
# f vlc [OPTION]... (hit enter, choose files)
|
||||
|
||||
f() {
|
||||
# Store the program
|
||||
program="$1"
|
||||
|
||||
# Remove first argument off the list
|
||||
shift
|
||||
|
||||
# Store option flags
|
||||
options="$@"
|
||||
if [ -z "${options}" ]; then
|
||||
options=" "
|
||||
else
|
||||
options=" $options "
|
||||
fi
|
||||
|
||||
# Store the arguments from fzf
|
||||
arguments=($(fzf --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 ! [[ $program =~ ^(cd)$ ]]; then
|
||||
$program$options${arguments[@]} &
|
||||
else
|
||||
$program$options${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 ! [[ "$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
|
||||
echo $program$options$arguments >> ~/.bash_history
|
||||
|
||||
# Reload the ~/.bash_history into the shell's active history
|
||||
history -r
|
||||
|
||||
# Clean up temporary variables
|
||||
rm /tmp/fzf_tmp
|
||||
```
|
||||
### Opening files
|
||||
|
||||
```sh
|
||||
|
||||
Reference in New Issue
Block a user