Created
March 10, 2025 21:22
-
-
Save firedynasty/55701f9d2d6d13dbf8ac32b84b1dda88 to your computer and use it in GitHub Desktop.
Cycling through files in Terminal like a high end Text Editor
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
i1() { | |
if [ $# -eq 1 ]; then | |
# Find matching files | |
selected_file=$(find . -maxdepth 1 -type f -iname "*$1*" | head -1) | |
if [ -n "$selected_file" ]; then | |
if is_audio_file "$selected_file"; then | |
echo "Opening in VLC: $selected_file" | |
open -a 'VLC' "$selected_file" | |
else | |
echo "Opening in default app: $selected_file" | |
open "$selected_file" | |
fi | |
else | |
echo "No matching files found." | |
fi | |
else | |
# No argument - use fzf to select a file | |
selected_file=$(find . -maxdepth 1 -type f -print0 | sort -z | fzf --read0) | |
if [ -n "$selected_file" ]; then | |
if is_audio_file "$selected_file"; then | |
echo "Opening in VLC: $selected_file" | |
open -a 'VLC' "$selected_file" | |
else | |
echo "Opening in default app: $selected_file" | |
open "$selected_file" | |
fi | |
else | |
echo "No file selected." | |
fi | |
fi | |
} | |
c1() { | |
# Fixed is_audio_file function with more compatible syntax | |
is_audio_file() { | |
local file="$1" | |
local ext=$(echo "${file##*.}" | tr '[:upper:]' '[:lower:]') | |
case "$ext" in | |
mp3|wav|flac|m4a|aac|ogg|wma) | |
return 0 # True - is an audio file | |
;; | |
*) | |
return 1 # False - not an audio file | |
;; | |
esac | |
} | |
# Add the ipynb file detection function with debug output | |
is_ipynb_file() { | |
local file="$1" | |
local ext=$(echo "${file##*.}" | tr '[:upper:]' '[:lower:]') | |
echo "Checking file: $file with extension: $ext" | |
[ "$ext" = "ipynb" ] | |
} | |
# Function to open a file with the appropriate application | |
open_file() { | |
local selected_file="$1" | |
# Ensure the file path is properly processed (remove null bytes if any) | |
selected_file=$(echo "$selected_file" | tr -d '\0') | |
echo "Processing file: $selected_file" | |
# Get the file extension more explicitly | |
local filename=$(basename "$selected_file") | |
local extension="${filename##*.}" | |
echo "Detected extension: $extension" | |
if is_audio_file "$selected_file"; then | |
echo "Opening in VLC: $selected_file" | |
open -a 'VLC' "$selected_file" | |
elif [ "$extension" = "ipynb" ]; then | |
echo "Jupyter notebook detected, opening in Typora: $selected_file" | |
# Try alternative methods to open with Typora | |
open -a "Typora" "$selected_file" || open -a "/Applications/Typora.app" "$selected_file" | |
else | |
echo "Opening in default app: $selected_file" | |
open "$selected_file" | |
fi | |
} | |
local continue_loop=true | |
local search_term="$1" | |
while $continue_loop; do | |
if [ -n "$search_term" ]; then | |
# Count matching files (case insensitive) | |
matches=$(find . -maxdepth 1 -type f -iname "*$search_term*" -print0 | tr '\0' '\n' | wc -l) | |
if [ "$matches" -eq 1 ]; then | |
# Exactly one match - open directly | |
selected_file=$(find . -maxdepth 1 -type f -iname "*$search_term*") | |
open_file "$selected_file" | |
elif [ "$matches" -gt 1 ]; then | |
# Multiple matches - use fzf | |
selected_file=$(find . -maxdepth 1 -type f -iname "*$search_term*" -print0 | sort -z | fzf --read0 --print0 --prompt="Select a file (Esc to exit): ") | |
if [ -n "$selected_file" ]; then | |
# Print the selected file for debugging | |
echo "Selected file: $selected_file" | |
open_file "$selected_file" | |
else | |
echo "No file selected." | |
continue_loop=false | |
break | |
fi | |
else | |
echo "No matching files found." | |
continue_loop=false | |
break | |
fi | |
else | |
# No argument - show all files in fzf | |
selected_file=$(find . -maxdepth 1 -type f -print0 | sort -z | fzf --read0 --print0 --prompt="Select a file (Esc to exit): ") | |
if [ -n "$selected_file" ]; then | |
open_file "$selected_file" | |
else | |
echo "No file selected." | |
continue_loop=false | |
break | |
fi | |
fi | |
# Continue automatically without asking | |
# Small pause to let the user see the results | |
sleep 1 | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment