Blog/2024-02-23/fzf: Difference between revisions

From Rest of What I Know
Created page with "<syntaxhighlight inline>fzf</syntaxhighlight> is a fuzzy-finder on the terminal. I use it for all sorts of stuff. Selecting a git branch: <syntaxhighlight lang=zsh> gco() { branch="$(git branch | grep -v '^\+' | fzf | tr -d '[:space:]' | sed 's/^\*//')" if -z "$branch" ; then echo "No branch selected" return 1 fi git checkout "$branch" } </syntaxhighlight> File:20240223-fzf-branch-switching.gif|thumb|alt=A GIF of the script above in use, selectin..."
 
No edit summary
 
Line 33: Line 33:
I admit I do use it with <syntaxhighlight inline>kubectl config use-context</syntaxhighlight> but side-effects will occur.
I admit I do use it with <syntaxhighlight inline>kubectl config use-context</syntaxhighlight> but side-effects will occur.
[[File:20240223-fzf-preview.gif|thumb|alt=The fzf pipeline above being used to list three different folders and how many entries they have|fzf being used to preview something]]
[[File:20240223-fzf-preview.gif|thumb|alt=The fzf pipeline above being used to list three different folders and how many entries they have|fzf being used to preview something]]
[[Category:Blog]]

Latest revision as of 05:54, 16 April 2024

fzf is a fuzzy-finder on the terminal. I use it for all sorts of stuff.

Selecting a git branch:

gco() {
  branch="$(git branch | grep -v '^\+' | fzf | tr -d '[:space:]' | sed 's/^\*//')"
  if [[ -z "$branch" ]]; then
    echo "No branch selected"
    return 1
  fi
	git checkout "$branch"
}
A GIF of the script above in use, selecting a branch, and deleting another
fzf in action for adding and removing git branches

You can also select multiple rows if you like

gbd() {
  branches=$(git branch | fzf --multi | sed 's/^\*//' | sed 's/\s*//')
  git branch -D ${branches}
}

And if you want a preview, you can get one too:

ls | fzf --preview 'ls -l {} | wc -l'

I admit I do use it with kubectl config use-context but side-effects will occur.

The fzf pipeline above being used to list three different folders and how many entries they have
fzf being used to preview something