Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created March 7, 2011 18:31
Show Gist options
  • Save cowboy/858932 to your computer and use it in GitHub Desktop.
Save cowboy/858932 to your computer and use it in GitHub Desktop.
Curl a remote file into vim, setting the filename based on the URL's filename.
#!/bin/bash
if [ -z "$1" -o "$1" = "-h" -o "$1" = "--help" ]; then cat <<EOF
curlvim - v0.5 - 3/7/2011
http://benalman.com/
echo "Usage: `basename "$0"` [curl_options...] url"
Curl a remote file into vim, setting the filename based on the URL's filename.
If a file extension can't be determined from the URL, .html will be used by
default. If you're just specifying a domain without scheme (eg. benalman.com),
use a trailing / to keep curlvim from using the TLD as the file extension.
The vim executable will be auto-magically determined by stripping any leading
"curl" off this script's filename. For example, call this script "curlgvim"
and gvim will be run. If this naming convention isn't used, the executable
will default to "vim".
Copyright (c) 2011 "Cowboy" Ben Alman
Dual licensed under the MIT and GPL licenses.
http://benalman.com/about/license/
EOF
exit; fi
# Determine vim executable name from this script's name or default to "vim".
BIN=`basename "$0" | perl -pe 's#^curl## or $_ = "vim";'`
# Get remote URL (the first parameter not starting with -).
for URL; do [[ "$URL" != -* ]] && break; done
# Strip querystring / fragment from URL.
URL=`echo "$URL" | perl -pe 's/[?#].*$//'`
# Determine file extension from the URL or default to "html"
EXT=`echo "$URL" | perl -pe 's#^.*[^/]+\.([^/]+)$#$1# or $_ = "html";'`
# Determine file name based on URL and determined extension.
FILE=`echo "$URL" | perl -pe 's#.*?([^/]+)/?$#$1#'`
FILE=`basename $FILE ".$EXT"`.$EXT
# Actually curl file into vim.
curl "$@" | $BIN -c ":f $FILE" -c ":filetype detect" -
# Load latest jQuery source into MacVim (-L follows redirects).
curlmvim -L rj3.net/jquery.js
# Load a website's source into vim.
curlvim benalman.com/
(*
* Google Chrome View Source in MacVim - v0.1 - 3/7/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*)
tell application "Google Chrome"
set currentUrl to URL of active tab of window 1
-- Obviously, you may need to change ~/.scripts/curlmvim in the following line.
do shell script "bash -l ~/.scripts/curlmvim " & quoted form of currentUrl & " &> /dev/null &"
end tell
@mathiasbynens
Copy link

Small typo at line 2 of usage.sh: it says “curlmvim” instead of curlvim. Just a heads up.

@cowboy
Copy link
Author

cowboy commented Mar 13, 2011

That's not a typo! The in-script usage instructions say:

The vim executable will be auto-magically determined by stripping any leading
"curl" off this script's filename. For example, call this script "curlgvim"
and gvim will be run. If this naming convention isn't used, the executable
will default to "vim"

Perhaps I could have made it more clear, but who doesn't like a puzzle?

(mvim is the name of the command-line script that comes with MacVim)

@mathiasbynens
Copy link

I see! Never used MacVim or mvim so I wouldn’t know. Sorry for the false alarm!

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