emacs: open file path under cursor fast

Perm URL with updates: http://ergoemacs.org/emacs/emacs_open_file_path_fast.html

Emacs has a command ffap. When your cursor is on a file path or URL, it'll open the file. This is extremely useful in shell ouputs or in HTML file.

One problem is that it'll prompt to confirm. This is annoying. Also, the command itself doesn't have a key.

You can write a similar command that doesn't need confirmation. Here's the code.

(defun open-file-at-cursor ()
  "Open the file path under cursor.
If there is text selection, uses the text selection for path.
If the path is starts with “http://”, open the URL in browser.
Input path can be {relative, full path, URL}.
This command is similar to `find-file-at-point' but without prompting for confirmation.
"
  (interactive)
  (let ( (path (thing-at-point 'filename)))
    (if (string-match-p "\\`https*://" path)
        (progn (browse-url path))
      (progn ; not starting “http://”
        (if (file-exists-p path)
              (progn (find-file path))
            (if (file-exists-p (concat path ".el"))
                (progn (find-file (concat path ".el")))
              (progn
                (when (y-or-n-p (format "file doesn't exist: 「%s」. Create?" path) )
                  (progn (find-file path ))) ) ) ) ) ) ))

Be sure to give it a easy key. 〔☛ Emacs: How to Define Keys

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?