DanielFGray/fzf-scripts

I modified your interactive ripgrep, here is the new version

bionicles opened this issue · 3 comments

thank you so much for sharing your awesome igr script on the web.
igr really helped me when i was dealing with bugs setting stuff up.

in the spirit of your LICENSE, here is the modified version of your code:

function irg() {
  declare preview='bat --color=always --style=header,numbers -H {2} {1} | grep -C3 {q}'

  while getopts ':l' x; do
    case "$x" in
      l) list_files=1
        preview='bat --color=always --style=header,numbers {1} | grep -C3 {q}'
        ;;
    esac
  done
  shift $(( OPTIND - 1 ))
  unset x OPTARG OPTIND

  rg --color=always -n ${list_files:+-l} "$1" 2> /dev/null |
  fzf -d: \
  --ansi \
  --query="$1" \
  --phony \
  --bind="change:reload:rg -n ${list_files:+-l} --color=always {q}" \
  --bind='enter:execute:echo "code -rg {1}:{2}" && code -rg {1}:{2}' \
  --preview="[[ -n {1} ]] && $preview" 
}

TLDR:

  1. I made it into a function and renamed it irg to match interactive ripgrep in my brain.
  2. I changed what happens when you hit "enter":
    --bind='enter:execute:echo "code -rg {1}:{2}" && code -rg {1}:{2}' \
    this just opens the relevant file in VS Code in your active window, and jumps to the relevant line.
    before it does so, it echos the command to the terminal just so you can see later what file and line you opened

-r -> --reuse-window
-g -> --goto <file:line[:character]>

closing, because i have no issue which needs action

Hm, but I need to run Vim after ... Would you like to make a PR?

ah just saw this @vitaly-zdanevich

# this is a nice working version of interactive ripgrep which is much simpler which i found on github and slightly modified
# https://github.com/DanielFGray/fzf-scripts/blob/master/igr
function irgv() {
  declare preview='bat --color=always --style=header,numbers -H {2} {1} | grep -C3 {q}'

  while getopts ':l' x; do
    case "$x" in
      l) list_files=1
        preview='bat --color=always --style=header,numbers {1} | grep -C3 {q}'
        ;;
    esac
  done
  shift $(( OPTIND - 1 ))
  unset x OPTARG OPTIND

  rg --color=always -n ${list_files:+-l} "$1" 2> /dev/null |
  fzf -d: \
  --ansi \
  --query="$1" \
  --phony \
  --bind="change:reload:rg -n ${list_files:+-l} --color=always {q}" \
  --bind='enter:execute:echo "vi {1}" && vi {1}' \
  --preview="[[ -n {1} ]] && $preview"
}
export -f irgv

here's one that does vi, i couldn't figure out the string concatenation to get the line numbers but i thought it could be {1}:{2}