←back to thread

539 points todsacerdoti | 1 comments | | HN request time: 0.279s | source
1. eisbaw ◴[] No.44364243[source]
I made my own fuzzy ripgrep, rgf. Works like rg but then pipes into fzf. When selected, your editor pops up on that line. Pretty handy.

        rgf is a function
        rgf ()
        {
            ( unset rg;
            function rg_normalize ()
            {
                sed -E 's#-(\x1b\[[0-9;]*m)*([0-9]+)(\x1b\[[0-9;]*m)*-#\1:\2:\3#g'
            };
            rg --color=always "${@:-${RG_LAST_ARGS[@]}}" --with-filename --line-number | rg_normalize | fzf --ansi --multi > /dev/shm/.tmp.fzf );
            cmd_fuzzy_editor $(cat /dev/shm/.tmp.fzf | awk -F':' '{file=$1; line=$2;} {print file, line}') > /dev/shm/.my_editor.source;
            source /dev/shm/.my_editor.source
        }




         type cmd_fuzzy_editor
        cmd_fuzzy_editor is a function
        cmd_fuzzy_editor ()
        {
            ( set -eu;
            {
                local file="$1";
                local line="$2"
            } 2> /dev/null;
            export EDITOR="${EDITOR:-vim}";
            export FUZZY_EDITOR="${FUZZY_EDITOR:-${EDITOR}}";
            case "$FUZZY_EDITOR" in
                "vi" | "vim" | "gvim" | "nvim" | "neovim" | "spacevim")
                    echo "$FUZZY_EDITOR" "${file}" "+${line}"
                ;;
                "emacs")
                    echo "$FUZZY_EDITOR" --no-splash "+${line}" "${file}"
                ;;
                "nano")
                    echo "$FUZZY_EDITOR" "+${line}" "${file}"
                ;;
                "nedit")
                    echo "$FUZZY_EDITOR" "${file}" -line "${line}"
                ;;
                "sublime_text")
                    echo "$FUZZY_EDITOR" --new-window "${file}:${line}"
                ;;
                "code" | "vscode")
                    echo "$FUZZY_EDITOR" --goto "${file}:${line}"
                ;;
                "notepadqq" | "nqq")
                    echo "$FUZZY_EDITOR" -line "${line}" "${file}"
                ;;
                "kate" | "leafpad" | "mousepad" | "gedit")
                    echo "$FUZZY_EDITOR" "${file}"
                ;;
                *)
                    echo 'No FUZZY_EDITOR found!' 1>&2
                ;;
            esac )
        }