Skip to content

Instantly share code, notes, and snippets.

@herbertjones
Created February 27, 2015 16:30
Show Gist options
  • Save herbertjones/6fb319c8a257658f93cb to your computer and use it in GitHub Desktop.
Save herbertjones/6fb319c8a257658f93cb to your computer and use it in GitHub Desktop.
open file at cursor
;; Stolen from http://xahlee.blogspot.com/2013/05/emacs-open-file-path-under-cursor-fast.html
(defun hfj/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 ))) ) ) ) ) ) ))
(define-key evil-normal-state-map (kbd "gf")
'hfj/open-file-at-cursor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment