Skip to content

Instantly share code, notes, and snippets.

@GarreauArthur
Last active January 15, 2018 22:06
Show Gist options
  • Save GarreauArthur/caf12287a4316c0d3526c855681ac6a9 to your computer and use it in GitHub Desktop.
Save GarreauArthur/caf12287a4316c0d3526c855681ac6a9 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ]
then
echo "Parameter 1 or 2 missing"
kill -INT $$
fi
i=0
array=()
for file in .* *; do # proper way to do it : https://unix.stackexchange.com/a/162589
filename=${file##*/}
base=${filename%.*}
ext=".${filename##*.}"
l_ext=${#ext}
l=${#2}
search=""
if [[ ${2:-l_ext} == $ext ]]; then # if user type the extension
search=${filename: -l}
else
search=${base: -l}
fi
if [[ $search == $2 ]]; then
array[$i]=$filename;
i=$((i+1))
fi
done
found=${#array[@]}
if [[ $found == "0" ]]; then
echo "No corresponding file found"
elif [[ $found == "1" ]]; then
$1 ${array[0]}
else
echo "too many files found :"
for a in ${array[*]}; do
echo "* ${a}";
done
fi
# ${variable#pattern} Trim the shortest match from the beginning
# ${variable##pattern} Trim the longest match from the beginning
# ${variable%pattern} Trim the shortest match from the end
# ${variable%%pattern} Trim the shortest match from the end
@GarreauArthur
Copy link
Author

Let's try to build a script that allow autocomplete from the end of the word. For example, if you have a couple of files with the same beginning :

  • 2018-1
  • 2018-2
  • 2018-3

You can just type ./end-autocomplete.sh vim 1, and the script will open 2018-1 in vim.

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