Skip to content

Instantly share code, notes, and snippets.

@tuan
Created March 18, 2022 16:38
Show Gist options
  • Save tuan/1be32b192341b8929c9754af25e79a21 to your computer and use it in GitHub Desktop.
Save tuan/1be32b192341b8929c9754af25e79a21 to your computer and use it in GitHub Desktop.
Fuzzy search file by content and open the selected file in the existing vscode instance
#!/usr/bin/env bash
# 1. Search for text in files using Ripgrep
# 2. Interactively restart Ripgrep with reload action
# 3. Open the file in existing vscode instance
RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
INITIAL_QUERY="${*:-}"
IFS=: read -ra selected < <(
FZF_DEFAULT_COMMAND="$RG_PREFIX $(printf %q "$INITIAL_QUERY")" \
fzf --ansi \
--disabled --query "$INITIAL_QUERY" \
--bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
--delimiter : \
--preview 'bat --color=always {1} --highlight-line {2}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
)
[ -n "${selected[0]}" ] && code -r -g "${selected[0]}:${selected[1]}"
@tuan
Copy link
Author

tuan commented Mar 18, 2022

Dependencies:

  1. fzf
  2. ripgrep
  3. bat
  4. vscode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment