Live data from Hacker News

So you've installed `fzf` – now what?

andrew-quinn.me

11–20 of 292 posts

Re: So you've installed `fzf` – now what?

#11
Two things I use fuzzy finder for:

1. flog: go to git branches I had checked out in the past quickly

2. pr: preview and check out PRs I've been assigned to review

    flog () {
     branch="$(
        git branch --sort=-committerdate --format="%(committerdate:relative)%09%(refname:short)%09%(subject)" \
        | column -ts $'\t' \
        | fzf \
        | sed 's/.*ago \+\([^ ]*\) .*/\1/'
      )" 
     git co $branch || (
      echo -n "git co $branch" | pbcopy
     )
    }

    errcho() {
      echo "$@" 1>&2
    }
    
    jq_query=$(cat  "$tmpFile"
    
      preview_command="\
        jq -r '.[\"{1}\"].body' $tmpFile \
        | pandoc -f gfm -t markdown      \
        | glow -s light -
      "
      pr_number=$(
        jq -r \
          'to_entries | map(.value.line) | join("
    ")' \
          "$tmpFile" \
        | column -t -s: \
        | fzf                            \
            --ansi                       \
            --delimiter=' '              \
            --preview="$preview_command" \
            --preview-window=up:80%      \
        | grep -o '^[0-9]\+'
      )
    
      if [ -n "$pr_number" ]; then
        errcho "checking out #$pr_number"
        gh pr checkout "$pr_number" && gh pr view --web "$pr_number" && git pull || echo -e "gh pr checkout $pr_number && gh pr view --web $pr_number && git pull"
      else
        errcho "canceled"
      fi
    }

Re: So you've installed `fzf` – now what?

#12
post #9

So I might aswell ask here: I want to search for a string using ripgrep, select one of the files from the result list using FZF and then open the selected file with VS code. > rg . | fzf How do I do this on windows (NOT Linux)?? *Note:* Assume I already have ripgrep, FZF & VS Code installed.

You open Total Commander in that folder, press Alt+F7, put your string into the lower search box and press search. It will give you the list of matching files, with F3 available for quick preview; then you right click the file you need and choose "Open with VS Code" from the context menu.

Sadly, you don't get the "context" (the content of actual matches) out of the box, you'll have to resort to double-tapping F3 while manually going down the file list. That's a downside, I fully admit that.

Re: So you've installed `fzf` – now what?

#13
Fantastic article.

I was exactly in the situation the article describes, I installed it, then, didn't find a use case.

But that's because Ubuntu doesn't come with completion enabled by default.

You have to add in your bashrc:

    if [ -e /usr/share/doc/fzf/examples/key-bindings.bash ]; then
      source /usr/share/doc/fzf/examples/key-bindings.bash
    fi
The fzf and ubuntu docs don't mention you have to.

Couldn't find easily how to do it with google, but chatgpt saved the day once again.

Re: So you've installed `fzf` – now what?

#14
post #9

So I might aswell ask here: I want to search for a string using ripgrep, select one of the files from the result list using FZF and then open the selected file with VS code. > rg . | fzf How do I do this on windows (NOT Linux)?? *Note:* Assume I already have ripgrep, FZF & VS Code installed.

You open Total Commander in that folder, press Alt+F7, put your string into the lower search box and press search. It will give you the list of matching files, with F3 available for quick preview; then you right click the file you need and choose "Open with VS Code" from the context menu. Sadly, you don't get the "context" (the content of actual matches) out of the box, you'll have to resort to double-tapping F3 whil…

I appreciate your answer but the whole point is to do everything from the command line. If I need to use another application, I can just open VS Code in the directory and search from there directly.

In the article, they provide this:

>rg . | fzf | cut -d ":" -f 1

What would be the easy windows equivalent of

> cut -d ":" -f 1

??

Re: So you've installed `fzf` – now what?

#17
fzf is super useful for navigating and switching git branches I realised today [1]

    function gbll(){
    local tags branches target
    branches=$(
      git --no-pager branch --sort=-committerdate \
        --format="%(if)%(HEAD)%(then)%(else)%(if:equals=HEAD)%(refname:strip=3)%(then)%(else)%1B[0;34;1mbranch%09%1B[m%(refname:short)%(end)%(end)" \
      | sed '/^$/d') || return
    tags=$(
      git --no-pager tag | awk '{print "\x1b[35;1mtag\x1b[m\t" $1}') || return
    target=$(
      (echo "$branches"; echo "$tags") |
      fzf --no-hscroll --no-multi -n 2 \
          --ansi --preview="git --no-pager log -150 --pretty=format:%s '..{2}'") || return
    git checkout $(awk '{print $2}' 
[1]https://github.com/junegunn/fzf/wiki/Examples#git

Re: So you've installed `fzf` – now what?

#18
post #3

Quick question. How do you set up the key bindings for the command prompt window on Windows? The control-r, alt-c, etc.

In my PowerShell profile I have the following -

  Remove-PSReadlineKeyHandler 'Ctrl+r'
  Remove-PSReadlineKeyHandler 'Ctrl+t'
  Import-Module PSFzf
All other shortcuts worked out of the box!

Re: So you've installed `fzf` – now what?

#19
I love how people share their fzf scripts whenever a new post comes along. I'll share some of my own.

First, some env variables for setting defaults:

    export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
    export FZF_DEFAULT_OPTS='--layout=reverse --inline-info'
    export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

And some neat commands:

1. A command for fuzzy jumping between workspaces with tree previews (depends on `tree`):

    export WORKSPACE_ROOT="$HOME/whatever-your-root-is"
    ws() {
      cd "$WORKSPACE_ROOT/`ls -a $WORKSPACE_ROOT | fgrep -v .DS_Store | fzf --preview '(cd $WORKSPACE_ROOT/{1}; pwd; tree -C -L 1 -I node_modules -I .DS_Store)'`"
    }

2. A command to be able to fuzzy search files with previews and syntax highlighting. Depends on bat being installed.

    alias fzp="fzf --preview 'bat --style=numbers --color=always --line-range :500 {}' --border --height='80%'"

3. A command to fuzzily run npm scripts with previews and syntax highlighting. Depends on bat and gojq, but you can sub gojq with jq. It does have a bug where it doesn't handle ":" characters in script keys well, but I'll fix that at some point.

    npz() {
      local script
      script=$(cat package.json | gojq -r '.scripts | keys[] ' | fzf --preview 'cat package.json | gojq -r ".scripts | .$(echo {1})" | bat -l sh --color always --file-name "npm run $(echo {1})" | sed "s/File: /Command: /"') && npm run $(echo "$script")
    }

4. A command for rapidly selecting an AWS profile. As a user of AWS SSO with many accounts/roles, this is a life-saver. I combine this with having my prompt show the currently selected role for added value.

    alias aws-profile='export AWS_PROFILE=$(sed -n "s/\[profile \(.*\)\]/\1/gp" ~/.aws/config | fzf)'
    alias ap="aws-profile"

5. A command for fuzzily finding and tailing an AWS cloudwatch log group. I didn't come up with this one, and I can't remember where I read about it, or I'd attribute it properly.

    awslogs() {
      export AWS_PROFILE=$(cat ~/.aws/config | awk '/^\[profile /{print $2}' | tr -d ']' | fzf)
      local log_group=$(aws logs describe-log-groups | gojq -r '.logGroups[].logGroupName' | fzf)
      aws logs tail "$log_group" --since 3h --follow --format=short
    }

I also have a few other commands related using fzf, but they're more bespoke and probably not useful to others.

Re: So you've installed `fzf` – now what?

#20
post #4

So, just like what fish shell gives out of the box :)

I'm a recent fish convert (from zsh) and I love it.

The only thing that annoys me is working with Android AOSP requires sourcing a bunch of bash functions that I don't feel like porting to fish so I'm occasionally required to drop into bash whereas with zsh and it's POSIX compatibility I could just source the bash functions and it would work fine. But fish's completions work much better out of the box and they have some useful features like being aware of your history in each directory.

Post reply on HN