DEV Community

Cover image for Using fzf to choose from a list of choices
MoniqueLive
MoniqueLive

Posted on

Using fzf to choose from a list of choices

Some scenarios I use fzf to help me pick from a list of choices

#1 - picking which docker images I'd like to remove

fzfrmimage () {
  # - list all docker images, skipping the first line
  # - feed these lines into fzf (user: TAB select/hit ENTER)
  # - format a list of 'image:tag's and pass it to `docker image rm` 
  images=$(docker image ls | tail -n +2 | fzf | sed -n 's/\(\S*\) *\(\S*\).*/\1:\2/p')
  docker image rm $(echo $images)
}
Enter fullscreen mode Exit fullscreen mode

#2 - picking from pygments themes (styles)

I used this as a oneliner (actually 2 for better legibility) in order to preview some sample code using the style.

pygmentize -L styles | sed -n 's/^* \(.*\):/\1/p' |\
  fzf --preview "pygmentize -P style={} -l python ~/prj/python/bored/main.py"
Enter fullscreen mode Exit fullscreen mode
  • list all styles
  • filter for lines that start with an asterisk
  • remove the trailing ':' character
  • feed the list into fzf with a sample code preview
  • user: TAB select / hit ENTER
  • fzf prints the selected themes
  • profit!

FZF is a super flexible tool that I'm always looking for ways to leveraging its powers. Thanks Junegunn Choi for creating it!

Top comments (0)