2

If I have a URL to a file (for example, this gist), what's the easiest way to fetch that file from Emacs and edit a copy of it? Is there a better option than M-! curl -n [insert URL here] RET?

I'd consider an answer "better" if it didn't rely on an external facility, for instance. Also if it used the remote name of the file as the local file.

I could certainly script up a solution to this myself but it seems like a pretty normal thing to do—is there existing functionality to do this within Emacs? Is there a pre-existing package?

camdez
  • 396
  • 3
  • 6
  • 1
    I personally like your solution, which is easy enough to incorporate into a custom function that has a prompt to type or paste the url, and then uses `start-process`, save the file, and open it up in a buffer -- about 5 lines of code and you have yourself a custom function, perhaps a few additional lines to choose a name -- e.g., default is the file name, or edit the name to something different in the minibuffer, choose the location to save the file, etc. – lawlist Apr 13 '15 at 22:06
  • 2
    Here is a link to a related thread entitled **Download a File with Emacs Lisp**: http://stackoverflow.com/a/4449823/2112489 – lawlist Apr 13 '15 at 22:23

2 Answers2

2

M-x browse-url-emacs RET URL RET

phils
  • 48,657
  • 3
  • 76
  • 115
  • This is a pretty good solution. The downsides I see are: I have to exit `read-only-mode` if I want to edit to edit the buffer contents, and I have to use `C-x C-w` to save the file because otherwise it will try to save back to the remote host. Also it seems to make a remote request when I kill the buffer (not sure why, could be a local issue with a hook). All in all quite good. – camdez Apr 14 '15 at 15:01
2
,----[ C-h f url-handler-mode RET ]
| url-handler-mode is an interactive autoloaded compiled Lisp function
| in `url-handlers.el'.
| 
| (url-handler-mode &optional ARG)
| 
| Toggle using `url' library for URL filenames (URL Handler mode).
`----
politza
  • 3,316
  • 14
  • 16
  • 4
    Just quoting the terse docstring for `url-handler-mode` makes this a poor answer. Could you please expand on how your answer relates to the question and how it solves the problem? –  Apr 14 '15 at 10:20
  • That doc string is pretty much useless...basically what it does (AFAICT) is that it makes commands like `find-file` aware of `http://`, `https://`, `file://`, and `nfs://` URLs (it does this by manipulating `file-name-handler-alist`). It's a pretty good solution! Downsides are: I have to jump out of `ido-find-file` (it seems to get confused), then clear the minibuffer, then yank, then yank-pop (to get past what I cleared...), kick it out of `read-only-mode` if I want to edit, use `C-x C-w` to write (otherwise it tries to write back to the server), and type out the file name manually. – camdez Apr 14 '15 at 14:42
  • Strangely it also makes a remote request when I kill the buffer. Not yet sure what that's about... – camdez Apr 14 '15 at 14:47