Skip to content

Instantly share code, notes, and snippets.

@StephenHwang
Last active October 3, 2022 07:13
Show Gist options
  • Save StephenHwang/9d86cbc91710ad4b2a2803a7b40672d3 to your computer and use it in GitHub Desktop.
Save StephenHwang/9d86cbc91710ad4b2a2803a7b40672d3 to your computer and use it in GitHub Desktop.
Use fzf to seach saved commands from a file and send to bash command-line

Bash + fzf saved_commands.md

To help with remembering bash commands and reducing typing long lines (i.e. a ssh destination), I wrote this short function and binding to save commands within a file and fzf search and send them to the terminal as needed. Comments can be appended to a command to aid in searching for actions that do not match the written command.

Requirements:

Set up

Include in your .bashrc:

# fzf saved commands and filter comments
saved_commands() {
  strip_comment="sed 's/#.*$//;/^$/d'"
  fzf_saved_commands="tac ~/.saved_commands.txt 2> /dev/null | fzf +m"
  eval $fzf_saved_commands | eval $strip_comment
}
bind '"\C-f": "$(saved_commands)\e\C-e\er\e^"'

Make a .saved_commands.txt file in $HOME. Here is an example of adding a saved command for extracting tar.gz files.

echo "tar -zxvf     # extract tar.gz files" >> ~/.saved_commands.txt

Usage

Open search in termal with ctrl-f and begin typing command. Hit enter to send command to termal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment