1. 22
  1.  

  2. 7

    Another powerful technique: readline bindings. This alias brings up a fzf-style file picker on Ctrl-F. Any picked filenames are inserted at the current cursor position:

    bind -x '"\C-f":"_skim_insert"'
    _skim_insert() {
        local files="$(sk --cmd='rg --files' --multi)"
        if [[ $? -eq 0 ]]; then
            local completions="$(python3 -c 'import sys, shlex; print(" ".join(shlex.quote(a.rstrip("\n")) for a in sys.argv[1].splitlines()))' "$files")"
            READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}${completions}${READLINE_LINE:$READLINE_POINT}"
            READLINE_POINT=$(( $READLINE_POINT + ${#READLINE_LINE} ))
        fi
    }
    

    This one picks a Git commit ID from git log on Ctrl-G:

    bind -x '"\C-g":"_git_commit_insert"'
    _git_commit_insert() {
        local files="$(sk --cmd='git log --oneline' --multi --reverse)"
        if [[ $? -eq 0 ]]; then
            local completions="$(python3 -c 'import sys, shlex; print(" ".join(shlex.quote(a.split()[0]) for a in sys.argv[1].splitlines()))' "$files")"
            READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}${completions}${READLINE_LINE:$READLINE_POINT}"
            READLINE_POINT=$(( $READLINE_POINT + ${#READLINE_LINE} ))
        fi
    }
    

    Really handy for git commit --fixup!

    These examples use Skim instead of fzf. It’s a Rust reimplementation. I use Skim instead of fzf because I can easily cargo install it along with Ripgrep.

    1. 1

      Thanks for pointing me at Skim, it’s an awesome tool. My new interactive git branch selector alias is:

      alias gco='git checkout $(sk --cmd "git branch" | tr -d "*")'
      
    2. 1

      Probably not the most efficient way to combine fzf and rg, but I use this function daily:

      fo () {
              local file
              if hash rg 2> /dev/null
              then
                      file="$(rg --no-line-number --no-heading "" | fzf -0 -1 | awk -F: '{print $1}')"
              else
                      file="$(grep --line-buffered --color=never -r "" $@ | fzf -0 -1 | awk -F: '{print $1}')"
              fi
              echo $file
              if [[ -n $file ]]
              then
                      $EDITOR $file
              fi
      }