Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 49 lines (36 sloc) 1.4 KB
#!/usr/bin/env bash
if [[ ! "$1" || "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
Curl a remote file into an editor.
echo "Usage: $(basename "$0") [curloptions...] url"
Curl a remote file into the editor of your choice. The filename will be based
on the URL's filename. If a file extension can't be determined from the URL,
index.html will be used.
The editor will be auto-magically determined by stripping any leading "curl"
off this script's filename. For example, call this script "curlmate" and "mate"
will be run.
Copyright (c) 2012 "Cowboy" Ben Alman
Licensed under the MIT license.
exit; fi
script="$(basename "$0")"
# Create tempfile.
tmp="$(mktemp "/tmp/$script.XXXXX")"
# Curl (using all specified options) to tempfile, storing url for later.
echo -e 'Fetching file...\n'
url="$(curl "$@" -o "$tmp" -w '%{url_effective}')"
# Remove leading http:// or https:// from url, and add index.html at the end if
# filename can't be determined.
path="$(echo "$url" | perl -pe's#^https?://##i;s#/$|(^[^/\s]+|/[^/.\s]+)$#$1/index.html#')"
dir="$cache_dir/$(dirname "$path")"
file="$dir/$(basename "$path")"
# Create directory if it doesn't exist.
[ -e "$dir" ] || mkdir -p "$dir"
# Copy tempfile to final file.
mv "$tmp" "$file"
# Open file in editor!
echo -e "\nOpening ~${file#$HOME}"
"$bin" "$file"