Created
September 3, 2024 09:37
-
-
Save helltone/c7ed8ead2d77e4aee02be8ebdd2df079 to your computer and use it in GitHub Desktop.
zsh search() function with rg/bat/fzf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| search() { | |
| if [ $# -lt 2 ]; then | |
| echo "Usage: search <search_term> <directory>" | |
| return 1 | |
| fi | |
| search_term="$1" | |
| directory="$2" | |
| selected=$(rg --color=always "$search_term" "$directory" | \ | |
| fzf --ansi --delimiter : \ | |
| --preview 'bat --color=always -n {1} | head -500' \ | |
| --bind 'enter:accept') | |
| if [ -n "$selected" ]; then | |
| file=$(echo "$selected" | cut -d: -f1) | |
| line=$(echo "$selected" | cut -d: -f2 | tr -dc '0-9') | |
| echo "Opening file: $file" | |
| echo "At line: $line" | |
| vim "$file" "+$line" | |
| fi | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment