Skip to content

Instantly share code, notes, and snippets.

@codequokka
Last active July 29, 2020 21:26
Show Gist options
  • Save codequokka/7ac70ea94c0de2c0fd7b1ea0948845a0 to your computer and use it in GitHub Desktop.
Save codequokka/7ac70ea94c0de2c0fd7b1ea0948845a0 to your computer and use it in GitHub Desktop.
Grep the string and open the file in the exact location where it exists on bash
# Grep the string and open the file in the exact location where it exists
rgv() {
if [ $# -eq 0 ]; then
echo 'Need a string to search for!'
echo 'Usage: rgv string'
return 1
fi
search_words=$@
search_result=$(
rg --smart-case --vimgrep $search_words | \
fzf --delimiter=':' \
--preview='bat --color=always {1} -H {2} -r `expr {2} - 1`:`expr {2} + 1`'
)
if [ "$search_result" != '' ]; then
file_path=`echo $search_result | awk -F':' '{print $1}'`
line_number=`echo $search_result | awk -F':' '{print $2}'`
column_number=`echo $search_result | awk -F':' '{print $3}'`
else
return
fi
nvim $file_path "+call cursor($line_number, $column_number)" "+set hlsearch | let @/ = \"$search_words\""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment