Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Use cURL to get a remote file into an editor (originally from here:
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) 2011 "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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.